Testing if string contains a substring
Klaus Alexander Seistrup
spam at magnetic-ink.dk
Wed Apr 23 12:11:31 EDT 2003
Frantisek Fuka wrote:
> I want a simple test to see if str contains any occurences of substr.
> I am currently using this:
>
> import string
> if string.find(str,substr) + 1:
> ...do something...
If you're using a newer version of Python (2.2+?), you should be able
to do something like this:
#v+
>>> contains = lambda haystack, needle: haystack.find(needle) >= 0
>>> contains('foo bar fly', 'foo')
1
>>> contains('foo bar fly', 'xyz')
0
>>>
#v-
// Klaus
--
><> http://pnx.dk/
More information about the Python-list
mailing list