Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Indexing strings

Reply
Thread Tools

Indexing strings

 
 
Fred
Guest
Posts: n/a
 
      03-04-2005
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
 
Reply With Quote
 
 
 
 
Patrick Useldinger
Guest
Posts: n/a
 
      03-04-2005
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
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      03-04-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/
 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      03-04-2005
> 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
 
Reply With Quote
 
Patrick Useldinger
Guest
Posts: n/a
 
      03-05-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic indexing (multi-dimensional-indexing) (probably my most important/valuable posting up to this date) Skybuck Flying C Programming 30 09-18-2011 11:29 PM
Indexing services under Windows XP SP2 - Can I disable MS Indexing Service to hasten Google's OR does Google Desktop uses this MS Indexing Service? ricardodefaria Computer Support 6 08-05-2007 04:14 AM
How much slower is dict indexing vs. list indexing? Emin Python 4 01-12-2007 02:40 PM
Indexing PDF's using MS Indexing Service C ASP General 3 10-17-2003 05:47 PM
Indexing PDF Files using MS Indexing Service C ASP .Net 0 10-17-2003 04:27 PM



Advertisments