RE Module Question.
Grant Edwards
grante at visi.com
Tue Nov 6 00:37:58 EST 2001
In article <yZJF7.2438$Ly1.2068267 at newsrump.sjc.telocity.net>, Adonis Vargas wrote:
> i am trying to fidn specific word within a string and am unable to
> findanything suitable for it,
>
> test = "this is a test"
> if "test" in test: print "found string."
You have two choices:
1) Use the index method: it returns integer if string is found,
throws exception if it isn't:
test = "this is a test"
try:
i = test.index("test")
print "found at position",i
except ValueError:
print "not found"
2) Use the find method: it returns -1 if string not found
test = "this is a test"
i = test.find("test")
if i >= 0:
print "found at position",i
else:
print "not found"
If all you want is a boolean test, then find() is probably
simpler option:
if test.find("test") >= 0:
print "found string"
--
Grant Edwards grante Yow! Youth of today! Join
at me in a mass rally
visi.com for traditional mental
attitudes!
More information about the Python-list
mailing list