Skip to main content

How to Join a List of Strings in Python with str.join

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A list of words is easy to build, but printing it gives brackets and quotes. str.join glues the strings together with any separator you choose: a comma, a space, a newline or nothing. This guide shows three separators and the one error to expect, with real output from Python 3.15.2.

Syntax​

Syntax
<separator>.join(<strings>)
PartWhat it means
<separator>The text placed between the items, such as ", ". It can be empty: ""
<strings>A list, tuple or other iterable of strings

Python's documentation: "Return a string which is the concatenation of the strings in iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects. The separator between elements is the string providing this method."

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

join_words.py
words = ["tent", "stove", "lantern"]

print(", ".join(words))
print(" | ".join(words))
print("".join(["a", "b", "c"]))

The join_words.py script in a macOS-style code window: a list of three words joined with a comma and space, then with a pipe, then three letters joined with an empty string.

Step 2: Run It​

Terminal
uv run python join_words.py
Output
tent, stove, lantern
tent | stove | lantern
abc

The output in a terminal: tent, stove, lantern; then tent | stove | lantern; then abc.

The separator appears only between items: there's no comma after lantern. An empty separator runs the strings together.

When the List Holds Numbers​

join accepts strings only:

join_numbers.py
numbers = [1, 2, 3]

print(", ".join(numbers))
Terminal
uv run python join_numbers.py
Output
Traceback (most recent call last):
File "/Users/you/source/python-basics/join_numbers.py", line 3, in <module>
print(", ".join(numbers))
~~~~~~~~~^^^^^^^^^
TypeError: sequence item 0: expected str instance, int found

Item 0, the 1, isn't a string. Convert each item first, for example with a list comprehension: ", ".join([str(n) for n in numbers]) gives 1, 2, 3.

Common Mistakes​

MistakeWhat happensDo this instead
Writing words.join(", ")AttributeError: 'list' object has no attribute 'join'Call it on the separator: ", ".join(words)
Joining numbersTypeError: sequence item 0: expected str instance, int foundConvert them with str() first

Troubleshooting​

SymptomLikely causeFix
The words run togetherThe separator is ""Use " " or ", "

FAQ​

How do I put each item on its own line?​

Use a newline as the separator: "\n".join(words).

Why not add the strings with + in a loop?​

You can, but join builds the result in one step and never adds a stray separator at the end.

Conclusion​

", ".join(words) turned ["tent", "stove", "lantern"] into tent, stove, lantern, a pipe and an empty string worked the same way, and a list of numbers raised the TypeError until each item was a string.

Additional Resources​

  • str.join — joining strings with a separator.