string.find first before location

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Wessle

    #1

    string.find first before location

    Hi

    I have a string like this

    text = "abc abc and Here and there"
    I want to grab the first "abc" before "Here"

    import string
    string.find(tex t, "Here") # <type int>

    I am having a problem with the next step.

    thanks
  • Ravi Teja

    #2
    Re: string.find first before location

    text[:text.find('Her e')].rfind('abc')

    Comment

    • Peter Otten

      #3
      Re: string.find first before location

      Gary Wessle wrote:
      [color=blue]
      > I have a string like this
      >
      > text = "abc abc and Here and there"
      > I want to grab the first "abc" before "Here"
      >
      > import string
      > string.find(tex t, "Here") # <type int>
      >
      > I am having a problem with the next step.[/color]

      These days str methods are preferred over the string module's functions.
      [color=blue][color=green][color=darkred]
      >>> text = "abc abc and Here and there"
      >>> here_pos = text.find("Here ")
      >>> text.rfind("abc ", 0, here_pos)[/color][/color][/color]
      4

      Peter

      Comment

      • Gary Wessle

        #4
        Re: string.find first before location

        Peter Otten <__peter__@web. de> writes:
        [color=blue]
        > Gary Wessle wrote:
        >[color=green]
        > > I have a string like this
        > >
        > > text = "abc abc and Here and there"
        > > I want to grab the first "abc" before "Here"
        > >
        > > import string
        > > string.find(tex t, "Here") # <type int>
        > >
        > > I am having a problem with the next step.[/color]
        >
        > These days str methods are preferred over the string module's functions.
        >[color=green][color=darkred]
        > >>> text = "abc abc and Here and there"
        > >>> here_pos = text.find("Here ")
        > >>> text.rfind("abc ", 0, here_pos)[/color][/color]
        > 4
        >
        > Peter[/color]

        and what about when python 3.0 is released and those depreciated
        functions like find and rfind are not supported. is there another
        solution which is more permanent?

        Comment

        • Peter Otten

          #5
          Re: string.find first before location

          Gary Wessle wrote:
          [color=blue][color=green]
          >> These days str methods are preferred over the string module's functions.
          >>[color=darkred]
          >> >>> text = "abc abc and Here and there"
          >> >>> here_pos = text.find("Here ")
          >> >>> text.rfind("abc ", 0, here_pos)[/color]
          >> 4
          >>
          >> Peter[/color]
          >
          > and what about when python 3.0 is released and those depreciated
          > functions like find and rfind are not supported. is there another
          > solution which is more permanent?[/color]

          I think the functions may go away, the methods will stay; so I'm confident
          the above will continue to work.

          Peter

          Comment

          • Serge Orlov

            #6
            Re: string.find first before location

            Peter Otten wrote:[color=blue]
            > Gary Wessle wrote:
            >[color=green][color=darkred]
            > >> These days str methods are preferred over the string module's functions.
            > >>
            > >> >>> text = "abc abc and Here and there"
            > >> >>> here_pos = text.find("Here ")
            > >> >>> text.rfind("abc ", 0, here_pos)
            > >> 4
            > >>
            > >> Peter[/color]
            > >
            > > and what about when python 3.0 is released and those depreciated
            > > functions like find and rfind are not supported. is there another
            > > solution which is more permanent?[/color]
            >
            > I think the functions may go away, the methods will stay; so I'm confident
            > the above will continue to work.[/color]

            find and rfind methods are in danger too. AFAIR they are to be replaced
            by partion and rpartition methods. People who are worried about future
            can continue to use index and rindex

            Comment

            • Peter Otten

              #7
              Re: string.find first before location

              Serge Orlov wrote:
              [color=blue]
              > Peter Otten wrote:[color=green]
              >> Gary Wessle wrote:
              >>[color=darkred]
              >> >> These days str methods are preferred over the string module's
              >> >> functions.
              >> >>
              >> >> >>> text = "abc abc and Here and there"
              >> >> >>> here_pos = text.find("Here ")
              >> >> >>> text.rfind("abc ", 0, here_pos)
              >> >> 4
              >> >>
              >> >> Peter
              >> >
              >> > and what about when python 3.0 is released and those depreciated
              >> > functions like find and rfind are not supported. is there another
              >> > solution which is more permanent?[/color]
              >>
              >> I think the functions may go away, the methods will stay; so I'm
              >> confident the above will continue to work.[/color]
              >
              > find and rfind methods are in danger too. AFAIR they are to be replaced
              > by partion and rpartition methods. People who are worried about future
              > can continue to use index and rindex[/color]

              I really should read those PEPs before posting.

              And just as I was starting to complain I noted that my original reply to
              Gary is buggy -- excluding the last letter in the string from the rfind()
              search if "Here" is not found is probably never the desired behaviour. So
              r/index() is indeed superior here.

              Peter

              Comment

              • Gary Wessle

                #8
                Re: string.find first before location

                "Serge Orlov" <Serge.Orlov@gm ail.com> writes:
                [color=blue]
                > Peter Otten wrote:[color=green]
                > > Gary Wessle wrote:
                > >[color=darkred]
                > > >> These days str methods are preferred over the string module's functions.
                > > >>
                > > >> >>> text = "abc abc and Here and there"
                > > >> >>> here_pos = text.find("Here ")
                > > >> >>> text.rfind("abc ", 0, here_pos)
                > > >> 4
                > > >>
                > > >> Peter
                > > >
                > > > and what about when python 3.0 is released and those depreciated
                > > > functions like find and rfind are not supported. is there another
                > > > solution which is more permanent?[/color]
                > >
                > > I think the functions may go away, the methods will stay; so I'm confident
                > > the above will continue to work.[/color]
                >
                > find and rfind methods are in danger too. AFAIR they are to be replaced
                > by partion and rpartition methods. People who are worried about future
                > can continue to use index and rindex[/color]

                except with index and rindex, if the search string is not present,
                they return a
                [
                Traceback (most recent call last):
                ValueError: substring not found
                ]

                ps. is there a online doc or web page where one enters a method and it
                returns the related docs?

                Comment

                • Kent Johnson

                  #9
                  Re: string.find first before location

                  Gary Wessle wrote:[color=blue]
                  > ps. is there a online doc or web page where one enters a method and it
                  > returns the related docs?[/color]

                  The index to the library reference is one place:


                  and of course help() in the interactive interpreter...

                  Kent

                  Comment

                  Working...