How does Python know what letter is?



How does Python know what letter is?


In the example below, letter is just the variable name we chose to represent each item in whatever the loop is iterating through.

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

Python doesn’t know what letter is, because it’s simply the name we chose for our loop variable. We could have named it pizza or character - anything. The name should be something that tells us what it is, though. letter accomplishes just that.

FAQ: Learn Python: A Day at the Supermarket - String Looping