Python join(): Join a List into a String
join() joins several strings into one, putting a separator string between them. No separator is added at the very start or the very end. Unusually, you call it with the dot on the separator string, not on the list.
separator.join(strings)
Joining a list into one string
The string before the dot goes between one value and the next. Join with the empty string "" and the values are simply written together.
words = ["red", "green", "blue"]print(", ".join(words))print(" ".join(words))print("".join(words))print("\n".join(words))red, green, blue red green blue redgreenblue red green blue
When you print several lines, printing them in one go with "\n".join(lines) instead of calling print many times is faster when there is a lot of output.
Joining a list of numbers
join joins strings only. For a list that holds numbers, turn each one into a string with map(str, …) or str(x) for x in … and pass that.
nums = [3, 1, 4]print(" ".join(map(str, nums)))print("+".join(str(x) for x in nums), "=", sum(nums))print(", ".join(f"{x:02d}" for x in nums))3 1 4 3+1+4 = 8 03, 01, 04
Pairing it with split
Cut with split and then join with a different separator, and you have changed the separator. Paired with split() with no arguments, it reduces several spaces in a row to one.
date = "2026-11-24"print("/".join(date.split("-"))) line = " too many spaces "print(" ".join(line.split()))print("".join(reversed("abc")))2026/11/24 too many spaces cba
With one value or none
With only one value there is no in-between, so the separator is not used. Joining an empty list gives an empty string. So you don't need to check the count separately.
print(", ".join(["only"]))print(repr(", ".join([])))print(", ".join({"a": 1, "b": 2}))print("-".join(("x", "y")))only '' a, b x-y
It doesn't have to be a list. Anything that gives strings one after another can be joined. Give it a dictionary and it joins the keys.
Common mistakes
Passing a list of numbers as it is
nums = [1, 2, 3]
print(" ".join(nums))
→ TypeError: sequence item 0: expected str instance, int found
Convert them to strings before you pass them: " ".join(map(str, nums)). If you only want to print, print(*nums) is shorter.
Putting the dot on the list
words = ["a", "b"]
print(words.join(", "))
→ AttributeError: 'list' object has no attribute 'join'
join is a string method. Turn the order around and write ", ".join(words).
Passing a single string
word = "abc"
print(", ".join(word))
→ Instead of abc, the separator ends up between every character
Give it a string and it joins each character. To keep a single word as it is, put it in a list and pass ", ".join([word]).