Testing if string contains a substring

Bengt Richter bokr at oz.net
Wed Apr 23 16:16:14 EDT 2003


On Wed, 23 Apr 2003 11:18:45 -0400, Oren Tirosh <oren-py-l at hishome.net> wrote:

>On Wed, Apr 23, 2003 at 03:44:57PM +0200, 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...
>> 
>> But it somehow looks "ugly" to me. Is there more "standard" way to test 
>> this?
>
>if haystack.count(needle):
>   ...
>
>"If the count of needles in haystack is nonzero then..."
>
Except it's unnecessary to count past one, so it's sometimes
going to be inefficient.

Regards,
Bengt Richter




More information about the Python-list mailing list