RE: How to find if a string contains another string

Discussion in 'Python' started by Borse, Ganesh, Oct 30, 2007.

  1. Sorry, am getting this error.

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

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: 'in <string>' requires character as left operand
    >>>
    >>> if smallone in bigstring:

    .... print 'ok'
    .... else:
    .... print 'nok'
    ....
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: 'in <string>' requires character as left operand

    Do I need to import any module?

    Thanks,
    Ganesh
    -----Original Message-----
    From: Gary Herron [mailto:]
    Sent: 30 October 2007 12:09
    To: Borse, Ganesh
    Cc:
    Subject: Re: How to find if a string contains another string

    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
    > ======================================================================
    > ========
    >
    >



    ==============================================================================
    Please access the attached hyperlink for an important electronic communications disclaimer:

    http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
    ==============================================================================
     
    Borse, Ganesh, Oct 30, 2007
    #1
    1. Advertising

  2. Borse, Ganesh

    sandipm Guest

    you can use "find" function...which gives index of occurrence of
    smaller one in bigger one
    and return -1 if does not exists..


    if bigstring.find(smallone) > -1:
    return true
    else:
    return false


    sandip




    On Oct 30, 9:15 am, "Borse, Ganesh" <>
    wrote:
    > Sorry, am getting this error.
    >
    > >>> bigstring="python anaconda boa cobra"
    > >>> smallone="boa"
    > >>> smallone in bigstring

    >
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > TypeError: 'in <string>' requires character as left operand
    >
    > >>> if smallone in bigstring:

    >
    > ... print 'ok'
    > ... else:
    > ... print 'nok'
    > ...
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > TypeError: 'in <string>' requires character as left operand
    >
    > Do I need to import any module?
    >
    > Thanks,
    > Ganesh
    >
    > -----Original Message-----
    > From: Gary Herron [mailto:]
    > Sent: 30 October 2007 12:09
    > To: Borse, Ganesh
    > Cc:
    > Subject: Re: How to find if a string contains another string
    >
    > 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
    > > ======================================================================
    > > ========

    >
    > ===========================================================================­===
    > Please access the attached hyperlink for an important electronic communications disclaimer:
    >
    > http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
    > ===========================================================================­===
     
    sandipm, Oct 30, 2007
    #2
    1. Advertising

  3. On Tue, 30 Oct 2007 12:15:49 +0800, "Borse, Ganesh"
    <> declaimed the following in
    comp.lang.python:

    >
    > Sorry, am getting this error.
    >
    > >>> bigstring="python anaconda boa cobra"
    > >>> smallone="boa"
    > >>> smallone in bigstring

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > TypeError: 'in <string>' requires character as left operand
    > >>>


    Old Python version... (I ran into that when creating some utilities
    to assist post-run analysis of the output from an Ada program unit test
    -- I have Python 2.4 on my Windows desktop, but the Ada stuff run on
    Solaris... the Solaris boxes are 2.2 or 2.3).

    As mentioned, you can use the string find method instead, though it
    isn't quite as clean.
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Oct 30, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Dyer
    Replies:
    5
    Views:
    666
  2. Replies:
    17
    Views:
    845
    Hiran Chaudhuri
    Dec 6, 2005
  3. Borse, Ganesh
    Replies:
    0
    Views:
    239
    Borse, Ganesh
    Oct 30, 2007
  4. tomaszewski.p
    Replies:
    4
    Views:
    810
    Tom Anderson
    Sep 12, 2008
  5. Knute Johnson
    Replies:
    0
    Views:
    420
    Knute Johnson
    Sep 11, 2008
Loading...

Share This Page