Text (strings) as a data type

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Text (strings) as a data type

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which one is a string?
A "cat"
B cat
2. How many characters are in the string "hi mum"?
A 6
B 5
3. Which one is text, not a number?
A "42"
B 42
4. What is the empty string?
A " " (quote marks with a space inside)
B "" (quote marks with nothing inside)
5. Which statement is true?
A A string can only hold letters.
B A string can hold letters, digits, symbols and spaces.

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1"cat" AA string: text made of the characters c, a and t, written inside quote marks
2"" B"a b"
3Digits inside quote marks are text, not a number to do sums with CThe closing quote mark is missing. A string needs a quote mark at the start and at the end: "hello".
4Spaces count as characters in a string, so this sentence has 3 characters D"5"
5name = cat EThe empty string: text with no characters inside the quote marks
6word = "hello FThe word cat has no quote marks, so it is not written as a string of text. It should be "cat".

Definitions

string

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
word = "dog" print(____(word))
2.
pin = "1234" print(len(___))
3.
greeting = "hi there" print(___(greeting))
4.
empty = "" print(len(empty) == _)
5.
num = "5" print(type(num) == ___)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A name = "Ava"
B pet = "cat"
C print(name + " has a " pet)
Question 2
A word = "cat"
B phrase = "hi there
C print(phrase)
Question 3
A code = "2024"
B total = code + 1
C print(total)
Question 4
A first = "go"
B space = " "
C msg = first + "now"
D print(msg)
Question 5
A text = ""
B text = text + "a"
C text = text + "b"
D print(len(text)

Answer Key

Text (strings) as a data type
Section A: Quick Questions
Q1
A — "cat"
Q2
A — 6
Q3
A — "42"
Q4
B — "" (quote marks with nothing inside)
Q5
B — A string can hold letters, digits, symbols and spaces.
Section B: Match the Pairs
Q1
"cat" → A string: text made of the characters c, a and t, written inside quote marks
Q2
"" → The empty string: text with no characters inside the quote marks
Q3
Digits inside quote marks are text, not a number to do sums with → "5"
Q4
Spaces count as characters in a string, so this sentence has 3 characters → "a b"
Q5
name = cat → The word cat has no quote marks, so it is not written as a string of text. It should be "cat".
Q6
word = "hello → The closing quote mark is missing. A string needs a quote mark at the start and at the end: "hello".
Section C: Definitions
Q1
string — A string is text: a row of characters such as letters, digits, symbols and spaces, written inside quote marks like "cat". Even "5" is a string.
Section D: Fill in the Blanks
Q1
word = "dog" print(type(word))
Q2
pin = "1234" print(len(pin))
Q3
greeting = "hi there" print(len(greeting))
Q4
empty = "" print(len(empty) == 0)
Q5
num = "5" print(type(num) == str)
Section E: Spot the Error
Q1
C — Pieces of text are joined with +. The + is missing before pet. It should be print(name + " has a " + pet).
Q2
B — A string needs a quote mark at both ends. The closing quote is missing. It should be phrase = "hi there".
Q3
B — Digits inside quote marks are text, so you cannot add 1 to them. Turn the text into a number first: total = int(code) + 1.
Q4
C — A space counts as a character. Leaving out the space variable makes the text "gonow". It should be msg = first + space + "now".
Q5
D — The brackets do not match. len( needs its own closing bracket and so does print(. It should be print(len(text)).