Indexing strings

Steve Holden steve at holdenweb.com
Fri Mar 4 18:05:52 EST 2005


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/



More information about the Python-list mailing list