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

  1. line = word * 3
    Repetition concatenates three full copies: 'py' * 3 โ†’ 'pypypy'. Not interleaved, not 'ppp yyy' โ€” block after block.
  2. print(line)
    Prints the built string: pypypy.
  3. print(len(line))
    len(s * n) = len(s) ร— n โ†’ 2 ร— 3 = 6.
  4. print(line[4])
    Index the RESULT: p-y-p-y-p-y at indexes 0โ€“5; index 4 is p.
  5. 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 โœ“

Keep practising this rule

More output questions โ†’ All interactive problems