Python For loop get index

for-loop, indexing, loops, python

Solution

Use the `enumerate()` function to generate the index along with the elements of the sequence you are looping over:

for index, w in enumerate(loopme):
    print "CURRENT WORD IS", w, "AT CHARACTER", index 

Problem

I am writing a simple Python for loop to prnt the current character in a string. However, I could not get the index of the character. Here is what I have, does anyone know a good way to get the current index of the character in the loop? ``` loopme = 'THIS IS A VERY LONG STRING WITH MANY MANY WORDS!' for w in loopme: print "CURRENT WORD IS " + w + " AT CHARACTER " ```

Original source

Related problems