0
0
Pythonprogramming~10 mins

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

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