getting an index in a for loop

Simon Bunker sibunks at hotmail.com
Fri Jan 31 08:43:22 EST 2003


I am iterating over a string and getting back each letter no problem.
eg.

>>> mystring = "abcdefg"
>>> for char in mystring:
...     print char
... 
a
b
c
d
e
f
g

which is fine, but is there any way of finding out the index into the
string we
are referencing? Or do I have to set up a temporary counter and
increment it in the loop each time?

I just have a feeling that Python should be cleverer than that - is
it?

Simon
http://www.rendermania.com/




More information about the Python-list mailing list