Testing if string contains a substring

Alex Martelli aleax at aleax.it
Wed Apr 23 17:54:35 EDT 2003


Jeremy Fincher wrote:

> Alex Martelli <aleax at aleax.it> wrote in message
> news:<NKxpa.85054$T34.2112670 at news2.tin.it>...
>> However, such new functionality doesn't get back-ported to previous
>> releases of Python, such as 2.2.*.  In all Python releases from 1,6
>> included to 2.3 excluded, "needle.find(haystack) > 0" is the idiom.
>> Module string is needed if you want to use antediluvian versions of
>> Python, such as 1.5.2, where strings didn't have methods.
> 
> Of course, you  meant "haystack.find(needle) >= 0", right?

Yes, sorry.  See my response to Bengt as to why I was confused
between needle-on-left and haystack-on-left;-).


> I always use (and have seen used) "haystack.find(needle) != -1", myself.

I never saw any added value in that one extra character -- if I did
want to use -1 I would also use > rather than != ...;-)


Alex





More information about the Python-list mailing list