Indexing strings

F

Fred

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
 
P

Patrick Useldinger

Fred said:
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
 
S

Steve Holden

Fred said:
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
 
F

Fred

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top