"str.contains(part)" or alternatives?

Stefan Schwarzer sschwarzer at sschwarzer.net
Wed Sep 11 17:03:15 EDT 2002


Hi Steve :)

Steve Holden wrote:
>> >>> s = 'Hello'
>> >>> if s.contains('ll'):
>>...     do_something_if_found()
>>... else:
>>...     do_something_if_not_found()
>>
>>Do you have other suggestions how to do the test easily without using special
>>values? Should a .contains method be part of string objects in future Python
>>versions? What do you think?
> 
> I think the current proposal is to move to
> 
>     if ll in s:
> 
> for this construct. Happy?

I would like .contains more, but at least the proposal is _much_ better than
"if s.find(part) != -1". :-)

"if part in s" disturbs the pattern "if item in sequence" somewhat but that has
already been weakened by "if key in dict" (that I like, nonetheless).

Stefan




More information about the Python-list mailing list