Class 9 Computer Science — Text inside a program
Work out how many characters a piece of text holds, which character sits at a given position, and what joining two pieces of text end to end gives.
Name: ________________________
1.Two pieces of text are joined and the length of the result is printed. What does this program print?
a = "web" b = "page" print(len(a + b))
- a) 4
- b) 8
- c) 2
- d) 7
2.This one works out the last position for itself before reading a character from it. What does it print?
word = "screen" print(word[len(word) - 1])
3.A loop can take a piece of text one character at a time, in the same way it takes a list one value at a time. What does this program print?
word = "data" count = 0 for c in word: count = count + 1 print(count)4.Sort each of these words by how many characters it has.
Groups: Four characters · Six characters
- loop
- file
- switch
- byte
- folder
- binary
5.The text is the same for all of these. Put them in order of the number each one gives, smallest first.
word = "printer"
- len(word) - 5
- len(word) - 3
- len(word)
- len(word) + 4
6.Two pieces of text are joined end to end here and the result is printed. What does the program print?
first = "sun" second = "rise" print(first + second)
7.The text is the same for all of these. Match each line to what it prints.
word = "network"
- print(word[0])
- print(word[3])
- print(word[6])
- print(len(word))
- print(word[len(word) - 2])
- 7
- k
- w
- r
- n
8.How many characters does the text in this program hold?
word = "keyboard" print(len(word))
Answer key — Class 9 Computer Science — Text inside a program
- 1. d) 7
- 2. n
- 3. 4
- 4. file → Four characters; folder → Six characters; byte → Four characters; binary → Six characters; loop → Four characters; switch → Six characters
- 5. 1. len(word) - 5 2. len(word) - 3 3. len(word) 4. len(word) + 4
- 6. sunrise
- 7. print(word[0]) → n; print(word[3]) → w; print(word[6]) → k; print(len(word)) → 7; print(word[len(word) - 2]) → r
- 8. 8