Newbie question - strings

Alex Martelli aleax at aleax.it
Mon May 19 08:33:56 EDT 2003


mandingo wrote:

> Sorry for my ignorance.
> 
> What is the Python equivelent to VB's NthField?

where in Basic you might have:

    somestring = "a,few,words,go,here"
    another = NthField(somestring, ",", 3)

in Python you might have:

    somestring = "a,few,words,go,here"
    another = somestring.split(",")[2]

Note that in the Python approach you're building a
whole list of fields then selecting a specific one
(0-based indexing like everywhere in Python) -- more
often than not you'll want more than one field so
you'll save the list and use it repeatedly, e.g.:

    somestring = "a,few,words,go,here"
    fields = somestring.split(",")
    afield = fields[0]
    another = fields[2]

and the like.  But, if you do want just one field,
you can choose to create and index the list at once.


Alex






More information about the Python-list mailing list