Indexing strings

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

    #1

    Indexing strings

    Hi everybody

    I am searching for a possibility, to find out, what the index for a
    certain lettyer in a string is.
    My example:

    for x in text:
    if x == ' ':
    list = text[: # There I need the index of the space the
    program found during the loop...

    Is there and possibility to find the index of the space???
    Thanks for any help!
    Fred
  • Patrick Useldinger

    #2
    Re: Indexing strings

    Fred wrote:
    [color=blue]
    > I am searching for a possibility, to find out, what the index for a
    > certain lettyer in a string is.
    > My example:
    >
    > for x in text:
    > if x == ' ':
    > list = text[: # There I need the index of the space the
    > program found during the loop...
    >
    > Is there and possibility to find the index of the space???
    > Thanks for any help!
    > Fred[/color]

    Use the index method, e.g.: text.index(' ').
    What exactly do you want to do?

    -pu

    Comment

    • Steve Holden

      #3
      Re: Indexing strings

      Fred wrote:[color=blue]
      > Hi everybody
      >
      > I am searching for a possibility, to find out, what the index for a
      > certain lettyer in a string is.
      > My example:
      >
      > for x in text:
      > if x == ' ':
      > list = text[: # There I need the index of the space the
      > program found during the loop...
      >
      > Is there and possibility to find the index of the space???
      > Thanks for any help!
      > Fred[/color]

      Perhaps you need something at a higher level (though you could use
      text.find(" ") for the first occurrence). I suspect you might want
      split(). Fred, meet split(). split(), meet Fred.
      [color=blue][color=green][color=darkred]
      >>> s = "The quick brown python swallows the lazy mongoose"
      >>> s.split()[/color][/color][/color]
      ['The', 'quick', 'brown', 'python', 'swallows', 'the', 'lazy', 'mongoose'][color=blue][color=green][color=darkred]
      >>> s.split(None)[/color][/color][/color]
      ['The', 'quick', 'brown', 'python', 'swallows', 'the', 'lazy', 'mongoose'][color=blue][color=green][color=darkred]
      >>> s.split(None, 3)[/color][/color][/color]
      ['The', 'quick', 'brown', 'python swallows the lazy mongoose'][color=blue][color=green][color=darkred]
      >>> s.split(None, 1)[/color][/color][/color]
      ['The', 'quick brown python swallows the lazy mongoose'][color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      regards
      Steve
      --
      Meet the Python developers and your c.l.py favorites March 23-25
      Come to PyCon DC 2005 http://www.pycon.org/
      Steve Holden http://www.holdenweb.com/

      Comment

      • Fred

        #4
        Re: Indexing strings

        > Use the index method, e.g.: text.index(' ').[color=blue]
        > What exactly do you want to do?[/color]

        That was exactely what I was searching for. I needed a program, that
        chopped up a string into its words and then saves them into a list. I
        think I got this done...
        Thanks for the help

        Comment

        • Patrick Useldinger

          #5
          Re: Indexing strings

          Fred wrote:
          [color=blue]
          > That was exactely what I was searching for. I needed a program, that
          > chopped up a string into its words and then saves them into a list. I
          > think I got this done...[/color]

          There's a function for that: text.split().
          You should really have a look at the Python docs. Also,
          http://diveintopython.org/ and http://www.gnosis.cx/TPiP/ are great
          tutorials.

          -pu

          Comment

          Working...