[Tutor] find

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Tue, 4 Dec 2001 21:28:49 -0800 (PST)


Oh, one other thing about string.find(): it will return the position of
where it finds a match, but if there isn't a match, it'll return '-1':

###
>>> string.find("james and the giant peach", "james")
0
>>> string.find("james and the giant peach", "peach")
20
>>> string.find("hat", "rabbit")
-1
###

so be careful when you use string.find() in an 'if' condition.



Here's an example of a common bug:

##
>>> if string.find("rosebud", "rosebud"):    ## This should have been
...     print "What happen?"                 ## if string.find("rosebud",
... else:                                    ##       "rosebud") == -1:
...     print "Someone set us up."
... 
Someone set us up.
###


Hope this helps!