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.
Jump into concepts and practice - no test required
a = "Hi" b = "!" c = a + b r = c * 3 print(r)
| Step | Variable | Operation | Value | Output |
|---|---|---|---|---|
| 1 | a | Assign | "Hi" | |
| 2 | b | Assign | "!" | |
| 3 | c | Concatenate a + b | "Hi!" | |
| 4 | r | Repeat c * 3 | "Hi!Hi!Hi!" | |
| 5 | print(r) | Output r | Hi!Hi!Hi! |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| a | "Hi" | "Hi" | "Hi" | "Hi" | |
| b | "!" | "!" | "!" | ||
| c | "Hi!" | "Hi!" | |||
| r | "Hi!Hi!Hi!" |
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.
+ operator do when used with strings in Python?+ operator with strings+ combines two strings by joining them end to end.*, uppercase uses .upper(), splitting uses .split().'hi' 3 times in Python?* repeats a string when multiplied by an integer.'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).result = 'ab' + 'cd' * 2 print(result)
text = 'go' * '3' print(text)
'yes' 4 times, separated by a dash -. Which code produces 'yes-yes-yes-yes'?