Testing if string contains a substring

Jp Calderone exarkun at intarweb.us
Wed Apr 23 10:09:49 EDT 2003


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?

  The preferred way is:

    if s.find(substr) != -1:
        ...

  In 2.3, this new way will work:

    if substr in s:
        ...

  Jp

-- 
"One World, one Web, one Program." - Microsoft(R) promotional ad
"Ein Volk, ein Reich, ein Fuhrer." - Adolf Hitler
-- 
 up 34 days, 11:03, 4 users, load average: 1.96, 1.78, 1.70





More information about the Python-list mailing list