String Repetition

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

String Repetition

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What is "*" * 5?
A * * * * *
B *****
2. What is "-" * 3?
A ---
B - - -
3. What is "Ha" * 3?
A HaHaHa
B Ha Ha Ha
4. What is "=" * 10?
A = = = = = = = = = =
B ==========
5. Does "*" * 5 add spaces?
A No
B Yes

Match the Pairs

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

Left columnRight column
1'Ha' * 2.5 AOutput: *****
2'abc' * 'abc' BOutput: HaHaHa
3line = '-' * 20 CValid: Repeats - 20 times
4'Ha' * 0 DError: Can't multiply str by str
5print('Ha' * 3) EOk: Returns empty string
6print('*' * 5) FError: Multiplier must be int

Definitions

"*" * 10

Fill in the Blanks

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

1.
line = "-" _ 20 # Output: --------------------
2.
stars = ___ * 10 # Output: **********
3.
border = "=" * __ # Output: ===============
4.
laugh = "Ha" * _ # Output: HaHaHa
5.
separator = 20 * ___ # Output: --------------------

Spot the Error

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

Question 1
A stars = "*" * 5
B line = "-" x 10
C print(line)
Question 2
A border = "=" * 20
B laugh = "Ha" * 3
C separator = - * 15
D print(separator)
Question 3
A stars = "*" * 10
B line = "-" * "5"
C print(line)
Question 4
A pattern = "ABC" * 3
B line = "-" * 20
C dots = "." 5
D print(dots)
Question 5
A border1 = "#" * 10
B border2 = 15 * "#"
C laugh = Ha * 3
D print(laugh)

Answer Key

String Repetition
Section A: Quick Questions
Q1
B — *****
Q2
A — ---
Q3
A — HaHaHa
Q4
B — ==========
Q5
A — No
Section B: Match the Pairs
Q1
'Ha' * 2.5 → Error: Multiplier must be int
Q2
'abc' * 'abc' → Error: Can't multiply str by str
Q3
line = '-' * 20 → Valid: Repeats - 20 times
Q4
'Ha' * 0 → Ok: Returns empty string
Q5
print('Ha' * 3) → Output: HaHaHa
Q6
print('*' * 5) → Output: *****
Section C: Definitions
Q1
"*" * 10 — * repeats the string - "*" * 10 gives **********.~~"-" * 5
Section D: Fill in the Blanks
Q1
line = "-" * 20 # Output: --------------------
Q2
stars = "*" * 10 # Output: **********
Q3
border = "=" * 15 # Output: ===============
Q4
laugh = "Ha" * 3 # Output: HaHaHa
Q5
separator = 20 * "-" # Output: --------------------
Section E: Spot the Error
Q1
B — Line 2 uses x instead of *. Should be: line = "-" * 10
Q2
C — Line 3 is missing quotes around -. Should be: separator = "-" * 15
Q3
B — Line 2 puts the number in quotes. Use: line = "-" * 5
Q4
C — Line 3 is missing *. Should be: dots = "." * 5
Q5
C — Line 3 is missing quotes. Should be: laugh = "Ha" * 3