str.join()
Glues the strings in a list (or any iterable) into one string, with the text you call it on placed between every piece.
str.join(iterable)join() is the opposite of str.split(): it takes many strings and turns them into one. The string you call it on is the glue that goes between the pieces, so ", ".join(names) puts a comma and a space between every name.
Use it whenever you build text out of parts: a CSV line from a list of values, a sentence from words, a file path, or many lines joined with a new line.
The syntax surprises almost everyone at first: join() belongs to the separator, not to the list. You write "-".join(parts), never parts.join("-"). And every item must already be a string, so numbers need str() first.
Parameters
| Parameter | What it is |
|---|---|
iterablerequired | The pieces to glue together: a list, tuple, generator or anything else you can loop over. Every item must be a string. |
Return value
A new string. If the iterable is empty, an empty string "".
Examples
1Join words with a space or a dash
words = ["learn", "python", "today"]
print(" ".join(words))
print("-".join(words))
print("".join(words))learn python today learn-python-today learnpythontoday
2Build a CSV line
The reverse of splitting a CSV line: put the values back together with commas.
row = ["Maria", "Lisbon", "34"]
line = ",".join(row)
print(line)Maria,Lisbon,34
3Join numbers: convert them to strings first
prices = [4.5, 12, 3.25]
text = ", ".join(str(p) for p in prices)
print(text)4.5, 12, 3.25
The generator str(p) for p in prices turns every number into a string just before join() uses it.
4Print a shopping list, one item per line
items = ["milk", "bread", "eggs"]
print("\n".join("- " + item for item in items))- milk - bread - eggs
5Reverse a word
word = "stressed"
print("".join(reversed(word)))desserts
reversed() gives the characters one by one from the end, and join() with an empty glue puts them back together.
Common mistakes
Calling join() on the list
Lists have no join() method, so words.join(" ") raises AttributeError. Put the separator first.
words = ["red", "green", "blue"]
try:
words.join(" ")
except AttributeError:
print("AttributeError: lists have no join()")
print(" ".join(words))AttributeError: lists have no join() red green blue
Joining numbers directly
join() refuses anything that is not a string and raises TypeError. Convert with str() or map(str, …) first.
numbers = [1, 2, 3]
try:
print("+".join(numbers))
except TypeError:
print("TypeError: join() needs strings, not numbers")
print("+".join(map(str, numbers)))TypeError: join() needs strings, not numbers 1+2+3
Questions people ask
How do I join a list with a comma and a space?
", ".join(items). The separator can be as long as you like.
Why is join() a string method and not a list method?
Because it works with any iterable (lists, tuples, generators, dictionary keys), and the result is always a string. Putting it on the separator means it only had to be written once.
Is join() faster than adding strings with +?
Yes, when there are many pieces. + in a loop builds a new string every round; join() measures everything once and builds the result in one go.
How do I join the keys of a dictionary?
Loop over the dictionary directly: ", ".join(prices) joins the keys, ", ".join(sorted(prices)) joins them in alphabetical order.