Grade 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 end to end here and the result is printed. What does the program print?
first = "sun" second = "rise" print(first + second)
2.A character is read out of the joined text rather than out of either of the two pieces. What does this program print?
left = "top" right = "most" joined = left + right print(joined[3])
3.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])
- k
- n
- 7
- w
- r
4.This program reads a single character out of a piece of text. What does it print?
word = "computer" print(word[0])
5.How many characters does the text in this program hold?
word = "keyboard" print(len(word))
6.Sort each of these words by how many characters it has.
Groups: Four characters · Six characters
- file
- folder
- loop
- binary
- byte
- switch
7.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) 8
- b) 7
- c) 2
- d) 4
8.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)
Answer key — Grade 9 Computer Science — Text inside a program
- 1. sunrise
- 2. m
- 3. print(word[0]) → n; print(word[3]) → w; print(word[6]) → k; print(len(word)) → 7; print(word[len(word) - 2]) → r
- 4. c
- 5. 8
- 6. file → Four characters; folder → Six characters; byte → Four characters; binary → Six characters; loop → Four characters; switch → Six characters
- 7. b) 7
- 8. 4