GoalUse a for loop with range(len(word)) to visit every character, and count or collect characters.
New words
traverse · to visit every item in a string or list, one at a time, from the first to the lastempty string · a string with no characters at all, written as ""concatenation · joining strings together with +
Explanation
To look at every character in a string, loop over its positions: for i in range(len(word)). The call len(word) gives the number of characters, and range stops one before it, so the counter takes exactly the valid indexes from 0 to len(word) - 1. Inside the loop, word[i] is the character at position i. An IF can compare it with a letter, for example word[i] == "a", and a counter variable can tally the matches. You can also build a new string. Start with an empty string, result = "", and join one character per pass. If each new character is put in front, result = word[i] + result, the word comes out backwards. Writing range(len(word) + 1) by mistake asks for one position too many and causes an IndexError.
Python
1word = "pear"2for i inrange(len(word)):
3print(i, word[i])
Counting spaces to count words. The user types I like green tea. The loop checks each character and adds 1 to spaces whenever it is a space. There are three spaces, so the program prints 4 words. (This only works when the words are separated by single spaces.)
Python
1word = input("Word? ")
2result = ""3for i inrange(len(word)):
4 result = word[i] + result
5print(result)
Output
aet
Reversing a word by joining characters at the front. The user types tea. The string result starts empty. Each pass puts the next character in front of what is already there. The output is aet.
i
word[i]
result
(before)
(empty)
0
t
t
1
e
et
2
a
aet
The same program traced for tea. In each pass the new character goes in front of result. The values of result are t, then et, then aet.
Class activity
Walk the word (no computer). Write a word of 6 letters on a strip of paper with one letter per box. Use a small counter as the variable i and move it along the boxes, saying the index and the letter at each move. Keep a tally for a letter you choose, such as e. Then build the backwards word on a second strip, adding each new letter at the left end. Try the word again with range(len(word) + 1) and see what happens at the last move.
Practice
1
What does this program print?
Python
1word = "sky"2for i inrange(len(word)):
3print(word[i], i)
s 0, then k 1, then y 2, each on its own line. The character comes first, then the index.
2
The user types banana. Trace the program and give the output. How would you change the program to count the letter n instead?
The letter a is at positions 1, 3 and 5, so count becomes 1, 2 and 3 and the output is 3. To count n, change the comparison to word[i] == "n" (the answer would then be 2).
3
The program prints c, a and t and then stops with an error. Name the error and say which change repairs it.
Python
1word = "cat"2for i inrange(len(word) + 1):
3print(word[i])
IndexError: string index out of range. When i is 3, word[3] does not exist because cat has indexes 0 to 2. Change range(len(word) + 1) to range(len(word)).
4
Write a program that asks for a word and prints Same if its first and last letters are the same, and Different if not. What should it print for level and for stone?
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): word = input("Word? ") / if word[0] == word[len(word) - 1]: / [indent] print("Same") / else: / [indent] print("Different"). Check: For level the first and last letters are both l, so it prints Same. For stone, s and e differ, so it prints Different.