can I get the index number in for x in y loop?

Paul McGuire ptmcg at austin.rr._bogus_.com
Mon Apr 3 11:38:37 EDT 2006


"JuHui" <phpbird at gmail.com> wrote in message
news:1144078372.411795.21570 at i39g2000cwa.googlegroups.com...
> >>> a='String'
> >>> for x in a:
> ...     print x
> ...
> S
> t
> r
> i
> n
> g
> >>>
>
> can I get the index number  of a in the upon loop within for x in a
> loop?
>

Use enumerate.  See example below.

-- Paul

>>> a = "String"
>>> for x in a:
...   print x
...
S
t
r
i
n
g
>>> for x in enumerate(a):
...   print x
...
(0, 'S')
(1, 't')
(2, 'r')
(3, 'i')
(4, 'n')
(5, 'g')
>>>





More information about the Python-list mailing list