Counting amount of items in Pythons 'for'
Posted
by
Markum
on Stack Overflow
See other posts from Stack Overflow
or by Markum
Published on 2012-06-02T22:27:40Z
Indexed on
2012/06/02
22:41 UTC
Read the original article
Hit count: 234
python
Kind of hard to explain, but when I run something like this:
fruits = ['apple', 'orange', 'banana', 'strawberry', 'kiwi']
for fruit in fruits:
print fruit.capitalize()
It gives me this, as expected:
Apple
Orange
Banana
Strawberry
Kiwi
How would I edit that code so that it would "count" the amount of times it's performing the for
, and print this?
1 Apple
2 Orange
3 Banana
4 Strawberry
5 Kiwi
© Stack Overflow or respective owner