0
0
Pythonprogramming~20 mins

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

Choose your learning style9 modes available
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.