Indexing strings

Discussion in 'Python' started by Fred, Mar 4, 2005.

  1. Fred

    Fred Guest

    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
     
    Fred, Mar 4, 2005
    #1
    1. Advertising

  2. Fred wrote:

    > 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


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

    -pu
     
    Patrick Useldinger, Mar 4, 2005
    #2
    1. Advertising

  3. Fred

    Steve Holden Guest

    Fred wrote:
    > 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


    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.

    >>> s = "The quick brown python swallows the lazy mongoose"
    >>> s.split()

    ['The', 'quick', 'brown', 'python', 'swallows', 'the', 'lazy', 'mongoose']
    >>> s.split(None)

    ['The', 'quick', 'brown', 'python', 'swallows', 'the', 'lazy', 'mongoose']
    >>> s.split(None, 3)

    ['The', 'quick', 'brown', 'python swallows the lazy mongoose']
    >>> s.split(None, 1)

    ['The', 'quick brown python swallows the lazy mongoose']
    >>>


    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/
     
    Steve Holden, Mar 4, 2005
    #3
  4. Fred

    Fred Guest

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


    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
     
    Fred, Mar 4, 2005
    #4
  5. Fred wrote:

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


    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
     
    Patrick Useldinger, Mar 5, 2005
    #5
    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. C
    Replies:
    0
    Views:
    501
  2. Emin
    Replies:
    4
    Views:
    410
    Paul McGuire
    Jan 12, 2007
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
  4. Skybuck Flying
    Replies:
    30
    Views:
    1,106
    Bill Reid
    Sep 19, 2011
  5. C
    Replies:
    3
    Views:
    222
    Manohar Kamath [MVP]
    Oct 17, 2003
Loading...

Share This Page