A loop variable receives a different item from a sequence on each repetition of a for loop. The loop body runs after each new value is assigned. section 16.4 section 16.3
Iterating over characters
for ch in "Python is fun so far!": print("the character is", ch) Here, ch is the loop variable. Python assigns it the first character, prints it, then assigns the next character and repeats until every character has been used. Spaces and punctuation count as characters too. section 17.2
A loop variable can take values from several kinds of sequences:
- range(3) assigns 0, then 1, then 2, so the loop runs three times. section 16.3
- A string assigns its characters directly, avoiding the need to use an index as a middleman. section 17.3
- A while loop requires you to create, check, and increment the comparable variable yourself; a for loop handles those steps automatically. section 18.2
Listing 17.1. A for loop that iterates over each character in a string
for ch in "Python is fun so far!": #1
print("the character is", ch) #2
Use the listing to trace how the loop variable changes from one character to the next. section 17.2

