Hone

Lessons · Python · join (pieces to text)

From pieces to one string

",".join(pieces) puts the separator between every item and returns one string. The separator is what you call it on.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

CSV lines, sentences from words, paths from parts, SQL from fragments. Concatenating in a loop with + is slow and leaves a stray separator; join is the right tool.

How to think about it

What are the pieces, and what goes between them? Get the pieces into a list of strings first (convert numbers with str). Then decide the separator, and call join on the separator.

Worked example

parts = ["2026", "09", "04"]
All strings.
print("-".join(parts))
2026-09-04.
nums = [1, 2, 3]
Numbers: join needs strings.
print(", ".join(str(n) for n in nums))
1, 2, 3.

Your turn

Make a sentence from words.

sentence = "".join(words)

The trap

Calling join on the list: parts.join(","). It is the other way round; the string owns join.

Practise join (pieces to text) on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.