At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
Hey Carl ! how are ya ? ;)
You need to show us what you're passing when you call the function, but looking at what you've posted: You don't need to import anything from string, .find() is a built in method, it's always available. When you call .find(), you have to pass a string as the first argument, which you have, but not the way I think you wanted to. Your function takes two arguments, which it assigns to a variable named target and one named key. When you call .find(), you must call it on a string, as in a = 'some big long string' b = a.find('substring') This assigns an integer to `b` which is the index position of the beginning of the first occurrence of the substring in `a`. You're calling the find method on the string assigned to 'a' when you do a.find() The method returns where in `a` the string passed to the method, in this case 'substring', occurs. It'll return -1 if the substring doesn't occur in `a`. You can pass two more optional arguments, but these must be integers, not a string, as you've got above. find('target', 'key') What you need to do is something like def findFirst (target, key): position = x.find(target, key) # note: no quotes, and were assuming that key is an int ## x = x+1 # you can't increment a string?? y = find(target, key, x) # again, no quotes print y return # what is this for? You only need a return statement if you're returning something other than None. Like return (x, y) If `x` is a string, doing y = 'abc' x.find(y, 5, 19) will look for where 'abc' first appears in the string, x[5:19]. There's a bunch of stuff to look at. Have a go at squaring things up a bit and let us know how you get on.
Hi grupiyati, I'm very well thank you, yourself?
Reading over my post I didn't make it very clear, let me know what bits you don't get and we'll go over them. That post is all over the place :)
What do you want the function to do? If you only want to find the first occurrence of one string in another, that's what the .find() method is for.
ok, now what I have is from string import * def fF(target,key): position = x.find(target,key) y = find(target,key,x) print y,x which I
which I totally understand ... but the error I'm getting is: >>> fF(abcd,bc) Traceback (most recent call last): File "
", line 1, in
NameError: name 'abcd' is not defined
which i dont understand :)
What you're doing is confusing strings with names. In your function, you have two names that are assigned to whenever it is called. The names, target and key, do not have 'quotes' because they are not strings of text, they are names. This is what you've done, so it's fine, but when you pass arguments to the function by doing >>>fF(abcd, bc) You're also using unquoted arguments, which defines them as names, not strings. If I have the following function def say_hi(nickname): print 'Hello', nickname you will notice that 'Hello' has quotes because it's a string, but nickname never has quotes, because it's a variable name. Now I can call the function, passing a string directly >>>say_hi('Bob') # note the quotes, it is a string I'm passing Hello Bob or I can pass a name that'll evaluate to a string >>>a = 'Bob' >>>say_hi(a) # note; no quotes. it is a variable I'm passing that evaluates to a string Hello Bob If I do >>>say_hi(Bob) I'll get NameError: name 'Bob' is not defined because the is no name Bob. So I could do this >>>Bob = 'Jim' >>>say_hi(Bob) Hello Jim
You don't need to import from string, there's nothing in your script that isn't built in. Have you looked at using IPython? It takes some setting up, but I'd be happy to help you get it running nicely for learning Python, assuming you're on Linux. IPython is a good tool for creating functions and playing around with them, the standard Python Interpreter's interactive mode really sucks.
You can't call find() as a function. It's a method, which is very similar to a function, but must be called on an instance of something ~ list methods must be called on a list, string methods are called on strings. Every string in your code has the string methods 'attached to it'. To call a method, you use the same parentheses and argument passing syntax and it works in the same way, but you must call the method 'on a string'. >>>a = 'abc' >>>print a.upper() ABC Or I could do >>>print 'abc'.upper() ABC but I can't do print upper(a) because upper is a string method and must be called on a string.
Sorry ~ I have just realised that find() is a function within the string module, so you can call it the way you were calling it before. Why they have a find function in string and a string method, I'll never know, but you can call find() the way you were doing it. If you are only pulling in one or two functions from a module, it can be better to name them from string import find I thought you'd just got the method call syntax wrong. Sorry about that. Chat soon :)
got it got it :) haha i'm at ps3b and c now :)
Good stuff mate.