Predict the output
What does this print?
parts = []
for i in range(4):
parts.append(str(i))
s = "-".join(parts)
print(s, len(s), s[::-1])Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 0-1-2-3 7 3-2-1-0
Right: join places the separator between items only.
join copies each piece once into a string of known size, which is why it beats repeated concatenation.
Why not the others?
- B 0-1-2-3- 8 -3-2-1-0. join does not add a trailing separator.
- C 0123 4 3210. The separator here is '-'.