Home โบ Output practice โบ String multiplication
Predict the output: string multiplication
๐ฎ Predict the outputTopic: stringsDifficulty: easyNo login needed
The snippet
String multiplication repeats the whole string โ and every later line operates on the result, not the original. The last line hides the edge case interviewers love: multiplying by zero. Predict all four printed lines.
word = "py"
line = word * 3
print(line)
print(len(line))
print(line[4])
print("-" * 0 + "!")
Your prediction
Type exactly what this program prints โ one line per print() call:
The output
pypypy
6
p
!
Line-by-line explanation
line = word * 3
Repetition concatenates three full copies:'py' * 3โ'pypypy'. Not interleaved, not 'ppp yyy' โ block after block.print(line)
Prints the built string:pypypy.print(len(line))
len(s * n) = len(s) ร n โ 2 ร 3 =6.print(line[4])
Index the RESULT: p-y-p-y-p-y at indexes 0โ5; index 4 isp.print("-" * 0 + "!")
Any string times zero (or a negative) is the empty string; '' + '!' โ!. A blank-looking line would have been the answer for print('-' * 0) alone.
Share your result
Challenge a friend โ the link contains the question, never the answer:
Copied โ