5. String Looping


Why is "i" printed twice?


This was my exact question too, I don't seem to catch why it would be printed if the letter 'i' isn't in either word. Hope someone can help out here.


This is the code given in the example:

word = "Programming is fun!"

for letter in word:
    # Only print out the letter i
    if letter == "i":
        print letter

A quick scan of the string, word reveals two (2) i's. The loop cycles through the string one letter at a time and compares it to the letter 'i', then prints when it finds a match. That is why there are two i's printed.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.