Search String for Word

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • digitalorganics@gmail.com

    #1

    Search String for Word

    What's the best way to search a string for a particular word and get a
    booleen value indicating whether it exists in the string or not?
    Thanks...

  • Tim Chase

    #2
    Re: Search String for Word

    > What's the best way to search a string for a particular word and get a[color=blue]
    > booleen value indicating whether it exists in the string or not?[/color]
    [color=blue][color=green][color=darkred]
    >>> substring = 'foo'
    >>> targetstring = 'blah foo bar'
    >>> substring in targetstring[/color][/color][/color]
    True[color=blue][color=green][color=darkred]
    >>> if substring in targetstring: print 'yup'[/color][/color][/color]
    yup



    -tkc




    Comment

    • digitalorganics@gmail.com

      #3
      Re: Search String for Word

      I thought I'd seen that somewhere! Thanks Tim. I was previously using
      re.search(subst ring, targetstring).

      Tim Chase wrote:[color=blue][color=green]
      > > What's the best way to search a string for a particular word and get a
      > > booleen value indicating whether it exists in the string or not?[/color]
      >[color=green][color=darkred]
      > >>> substring = 'foo'
      > >>> targetstring = 'blah foo bar'
      > >>> substring in targetstring[/color][/color]
      > True[color=green][color=darkred]
      > >>> if substring in targetstring: print 'yup'[/color][/color]
      > yup
      >
      > http://docs.python.org/lib/typesseq.html
      >
      > -tkc[/color]

      Comment

      • digitalorganics@gmail.com

        #4
        Re: Search String for Word

        And what if I want to search for an item in a tuple, is there a
        similarly easy method?

        Tim Chase wrote:[color=blue][color=green]
        > > What's the best way to search a string for a particular word and get a
        > > booleen value indicating whether it exists in the string or not?[/color]
        >[color=green][color=darkred]
        > >>> substring = 'foo'
        > >>> targetstring = 'blah foo bar'
        > >>> substring in targetstring[/color][/color]
        > True[color=green][color=darkred]
        > >>> if substring in targetstring: print 'yup'[/color][/color]
        > yup
        >
        > http://docs.python.org/lib/typesseq.html
        >
        > -tkc[/color]

        Comment

        • Tim Williams

          #5
          Re: Search String for Word

          On 26 Jun 2006 08:24:54 -0700, digitalorganics @gmail.com
          <digitalorganic s@gmail.com> wrote:[color=blue]
          > And what if I want to search for an item in a tuple, is there a
          > similarly easy method?
          >
          > Tim Chase wrote:[color=green][color=darkred]
          > > > What's the best way to search a string for a particular word and get a
          > > > booleen value indicating whether it exists in the string or not?[/color]
          > >[color=darkred]
          > > >>> substring = 'foo'
          > > >>> targetstring = 'blah foo bar'
          > > >>> substring in targetstring[/color]
          > > True[color=darkred]
          > > >>> if substring in targetstring: print 'yup'[/color]
          > > yup[/color][/color]
          [color=blue][color=green][color=darkred]
          >>> t = ('a', 'b', 'c', 'd', 'e')
          >>> a = 'a'
          >>> a in t[/color][/color][/color]
          True[color=blue][color=green][color=darkred]
          >>> y = 'y'
          >>> y in t[/color][/color][/color]
          False
          [color=blue][color=green][color=darkred]
          >>> t = ('test', 'black', 'white')
          >>> a = 'a'
          >>> [i for i in t if a in i][/color][/color][/color]
          ['black'][color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]

          HTH :)

          Comment

          • digitalorganics@gmail.com

            #6
            Re: Search String for Word

            Thank you thank you!

            Tim Williams wrote:[color=blue]
            > On 26 Jun 2006 08:24:54 -0700, digitalorganics @gmail.com
            > <digitalorganic s@gmail.com> wrote:[color=green]
            > > And what if I want to search for an item in a tuple, is there a
            > > similarly easy method?
            > >
            > > Tim Chase wrote:[color=darkred]
            > > > > What's the best way to search a string for a particular word and get a
            > > > > booleen value indicating whether it exists in the string or not?
            > > >
            > > > >>> substring = 'foo'
            > > > >>> targetstring = 'blah foo bar'
            > > > >>> substring in targetstring
            > > > True
            > > > >>> if substring in targetstring: print 'yup'
            > > > yup[/color][/color]
            >[color=green][color=darkred]
            > >>> t = ('a', 'b', 'c', 'd', 'e')
            > >>> a = 'a'
            > >>> a in t[/color][/color]
            > True[color=green][color=darkred]
            > >>> y = 'y'
            > >>> y in t[/color][/color]
            > False
            >[color=green][color=darkred]
            > >>> t = ('test', 'black', 'white')
            > >>> a = 'a'
            > >>> [i for i in t if a in i][/color][/color]
            > ['black'][color=green][color=darkred]
            > >>>[/color][/color]
            >
            > HTH :)[/color]

            Comment

            Working...