AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Data in detail

Week 7 · 1.5 Programming with data · 3 lessons
3

Walk along a word

Computing 9: Topic 1.5, pp. 89–118
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 in range(len(word)):
3    print(i, word[i])
Output
0 p
1 e
2 a
3 r
Worked examples
Python
1sentence = input("Sentence? ")
2spaces = 0
3for i in range(len(sentence)):
4    if sentence[i] == " ":
5        spaces = spaces + 1
6print("Words:", spaces + 1)
Output
Words: 4
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 in range(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.
iword[i]result
(before)(empty)
0tt
1eet
2aaet
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 in range(len(word)):
3    print(word[i], i)
2
The user types banana. Trace the program and give the output. How would you change the program to count the letter n instead?
Python
1word = input("Word? ")
2count = 0
3for i in range(len(word)):
4    if word[i] == "a":
5        count = count + 1
6print(count)
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 in range(len(word) + 1):
3    print(word[i])
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?