How to find if a string contains another string

Gary Herron gherron at islandtraining.com
Tue Oct 30 00:08:48 EDT 2007


Borse, Ganesh wrote:
> Hi,
>
> Am new to python.
> May someone please help me know this?
>
> How can we check whether one big string contains another small string?
>
> E.g. 
> bigstring="python anaconda boa cobra"
> smallone="boa"
>   
Use the operator named in:

>>> bigstring="python anaconda boa cobra"
>>> smallone="boa"
>>> smallone in bigstring
True

Often used in if statements like this:

>>> if smallone in bigstring:
...   print 'contained'
... else:
...   print 'not'
...
contained

Gary Herron

> If 0 == contains(bigstring,smallone):
>         print "Yes, boa is snake.."
>
> Is there any function like "contains" or "exists" in python?
> What are different alternatives we have?
>
> Please help.
>
> Thanks and Regards,
> Ganesh
>
> ==============================================================================
> Please access the attached hyperlink for an important electronic communications disclaimer: 
>
> http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
> ==============================================================================
>
>   




More information about the Python-list mailing list