Bird
Raised Fist0
Pythonprogramming~20 mins

String concatenation and repetition in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
String Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of repeated string concatenation
What is the output of this Python code?
Python
s = 'ab'
result = s * 3 + 'cd'
print(result)
A"abcdabcdabcd"
B"ababab"
C"abababcd"
D"ababcdcdcd"
Attempts:
2 left
๐Ÿ’ก Hint
Remember that multiplying a string repeats it, and + adds strings together.
โ“ Predict Output
intermediate
2:00remaining
Result of mixed string operations
What does this code print?
Python
a = 'x'
b = 'y'
print((a + b) * 2)
A"xyxy"
B"xxyy"
C"xy2"
D"x2y2"
Attempts:
2 left
๐Ÿ’ก Hint
The parentheses affect the order of operations.
โ“ Predict Output
advanced
2:00remaining
Output of string repetition with variables
What is the output of this code?
Python
word = 'go'
count = 4
print(word * count + word)
A"gogogogogog"
B"gogogogo"
C"gogogo4"
D"gogogogogo"
Attempts:
2 left
๐Ÿ’ก Hint
Multiplying a string by a number repeats it that many times.
โ“ Predict Output
advanced
2:00remaining
String concatenation with different types
What happens when you run this code?
Python
s = 'num'
n = 3
print(s + n * 2)
ATypeError
B"numnum"
C"num6"
D"num3num3"
Attempts:
2 left
๐Ÿ’ก Hint
You cannot add a string and an integer directly.
๐Ÿง  Conceptual
expert
3:00remaining
Understanding string repetition and concatenation order
Given the code below, what is the value of variable result?
Python
result = 'a' + 'b' * 2 + 'c' * 3
A"ab2c3"
B"abbccc"
C"aabcc"
D"abcabcabc"
Attempts:
2 left
๐Ÿ’ก Hint
Multiplication repeats the string before concatenation.

Practice

(1/5)
1. What does the + operator do when used with strings in Python?
easy
A. Joins two strings together into one
B. Repeats the string multiple times
C. Converts the string to uppercase
D. Splits the string into a list

Solution

  1. Step 1: Understand the + operator with strings

    In Python, + combines two strings by joining them end to end.
  2. Step 2: Compare with other options

    Repeating strings uses *, uppercase uses .upper(), splitting uses .split().
  3. Final Answer:

    Joins two strings together into one -> Option A
  4. Quick Check:

    String + String = Joined String [OK]
Hint: Remember: + joins strings, * repeats strings [OK]
Common Mistakes:
  • Confusing + with * for repetition
  • Thinking + changes string case
  • Assuming + splits strings
2. Which of the following is the correct syntax to repeat the string 'hi' 3 times in Python?
easy
A. 'hi' + 3
B. 'hi' * 3
C. 3 'hi'
D. 'hi' ** 3

Solution

  1. Step 1: Identify the repetition operator

    In Python, * repeats a string when multiplied by an integer.
  2. Step 2: Check other options

    'hi' + 3 causes TypeError (can't add string and int). 3 'hi' is SyntaxError (missing * operator). 'hi' ** 3 causes TypeError (** is for exponents, not string repetition).
  3. Final Answer:

    'hi' * 3 -> Option B
  4. Quick Check:

    String * Number = Repeated String [OK]
Hint: Use * between string and number to repeat [OK]
Common Mistakes:
  • Using + instead of * for repetition
  • Trying to use ** which is invalid for strings
  • Placing number before string without *
3. What is the output of the following code?
result = 'ab' + 'cd' * 2
print(result)
medium
A. abcdabcd
B. ababcd
C. abcdcdcd
D. abcdcd

Solution

  1. Step 1: Evaluate the repetition part

    'cd' * 2 repeats 'cd' twice, resulting in 'cdcd'.
  2. Step 2: Concatenate strings

    'ab' + 'cdcd' joins to form 'abcdcd'.
  3. Final Answer:

    abcdcd -> Option D
  4. Quick Check:

    'ab' + ('cd' * 2) = 'abcdcd' [OK]
Hint: Calculate repetition first, then join strings [OK]
Common Mistakes:
  • Adding strings before repeating
  • Repeating the whole concatenated string
  • Miscounting repeated parts
4. Find the error in this code snippet:
text = 'go' * '3'
print(text)
medium
A. TypeError because string cannot be multiplied by string
B. SyntaxError due to missing parentheses
C. NameError because 'text' is undefined
D. No error, prints 'gogogo'

Solution

  1. Step 1: Identify the operands of the * operator

    The code tries to multiply a string 'go' by another string '3'.
  2. Step 2: Understand Python's type rules for * operator

    Python allows string * integer but not string * string, causing a TypeError.
  3. Final Answer:

    TypeError because string cannot be multiplied by string -> Option A
  4. Quick Check:

    String * String = TypeError [OK]
Hint: Multiply string only by integer, not by string [OK]
Common Mistakes:
  • Using quotes around number for repetition
  • Assuming implicit conversion to int
  • Ignoring error messages
5. You want to create a string that repeats the word 'yes' 4 times, separated by a dash -. Which code produces 'yes-yes-yes-yes'?
hard
A. 'yes' + '-' * 3
B. 'yes-' * 4
C. ('yes-' * 4)[:-1]
D. '-'.join('yes' * 4)

Solution

  1. Step 1: Understand the desired output

    The string should be 'yes-yes-yes-yes' with 3 dashes separating 4 'yes'.
  2. Step 2: Analyze each option

    ('yes-' * 4)[:-1] repeats 'yes-' 4 times: 'yes-yes-yes-yes-' then removes last dash with [:-1]. 'yes-' * 4 leaves trailing dash. '-'.join('yes' * 4) joins characters, not words. 'yes' + '-' * 3 adds only one dash.
  3. Final Answer:

    ('yes-' * 4)[:-1] -> Option C
  4. Quick Check:

    Repeat with dash, then remove last dash [OK]
Hint: Repeat with dash, then remove last dash [OK]
Common Mistakes:
  • Leaving extra dash at end
  • Joining characters instead of words
  • Adding fewer dashes than needed