Predict the output
What does this print?
parts = [str(i) for i in range(5)]
joined = "-".join(parts)
built = ""
for p in parts:
built += p + "-"
print(joined, built.rstrip("-") == joined)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 0-1-2-3-4 True
Right: both build the same text; join does it in one pass.
Both strings are identical; the difference is how much copying happens on the way, which matters for big inputs.
Why not the others?
- B 0-1-2-3-4- True. join only puts the separator between items.
- C 01234 False. The separator is '-', so it appears between digits.