Bird
Raised Fist0
Pythonprogramming~10 mins

String concatenation and repetition in Python - Step-by-Step Execution

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
Concept Flow - String concatenation and repetition
Start with strings
Concatenate strings with +
Repeat string with *
Resulting string output
Start with strings, combine them using + to join, or * to repeat, then get the final string.
Execution Sample
Python
a = "Hi"
b = "!"
c = a + b
r = c * 3
print(r)
Joins 'Hi' and '!' then repeats the result 3 times and prints it.
Execution Table
StepVariableOperationValueOutput
1aAssign"Hi"
2bAssign"!"
3cConcatenate a + b"Hi!"
4rRepeat c * 3"Hi!Hi!Hi!"
5print(r)Output rHi!Hi!Hi!
💡 Program ends after printing the repeated concatenated string.
Variable Tracker
VariableStartAfter 1After 2After 3Final
a"Hi""Hi""Hi""Hi"
b"!""!""!"
c"Hi!""Hi!"
r"Hi!Hi!Hi!"
Key Moments - 2 Insights
Why does using + join strings instead of adding numbers?
The + operator joins strings by putting them side by side, as shown in step 3 where a + b becomes "Hi!" (see execution_table row 3). It does not add numeric values here.
What happens when you multiply a string by a number?
Multiplying a string by a number repeats the string that many times, as in step 4 where c * 3 becomes "Hi!Hi!Hi!" (see execution_table row 4). It does not multiply characters.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of variable c at step 3?
A"Hi"
B"Hi!"
C"!"
D"Hi!Hi!Hi!"
💡 Hint
Check the 'Value' column for step 3 in the execution_table.
At which step does the string get repeated three times?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the operation 'Repeat c * 3' in the execution_table.
If we change r = c * 2 instead of c * 3, what would be the output at step 5?
A"Hi!Hi!"
B"Hi!Hi!Hi!"
C"Hi!"
D"Hi!Hi!Hi!Hi!"
💡 Hint
Refer to variable_tracker for r's final value and how repetition count affects output.
Concept Snapshot
String concatenation uses + to join strings side by side.
String repetition uses * with an integer to repeat the string.
Example: 'Hi' + '!' = 'Hi!'
'Hi!' * 3 = 'Hi!Hi!Hi!'
Use print() to see the final string.
Full Transcript
This example shows how to join two strings using the plus sign and then repeat the joined string multiple times using the multiplication sign. First, variable a is assigned the string 'Hi'. Then b is assigned '!'. Next, c is created by joining a and b, resulting in 'Hi!'. Then r repeats c three times to get 'Hi!Hi!Hi!'. Finally, printing r outputs the repeated string. The plus sign joins strings side by side, and the multiplication sign repeats the string the given number of times.

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