Python String join() Method
separator.join(iterable) glues every string in the iterable together into one string, placing separator between neighbours.join() is the standard way to turn a list of words into a sentence, build a comma-separated line, or assemble a path-like string. It is called on the separator, not on the list, which surprises many beginners: ", ".join(names) rather than names.join(", ").
It is also the opposite of str.split(), and it is much faster than adding strings together with + in a loop.
| Syntax | separator.join(iterable) |
|---|---|
| Parameters | iterable: any iterable whose items are all str (list, tuple, set, generator, ...) |
| Returns | A new str |
| Time complexity | O(n) in the total length of the result |
How join() works and why the separator comes first
Python reads the iterable once, checks that every item is a string, and builds the result in a single pass. The separator appears only between items, never at the start or end. An empty separator, "", simply concatenates the pieces.
words = ["learn", "python", "today"]
print(" ".join(words))
print("-".join(words))
print("".join(words))Joining numbers and other non-string items
join() does not convert anything for you. If the list holds numbers, convert each item first, usually with map(str, items) or a generator expression. See type casting for the conversion functions.
scores = [82, 91, 77]
print(", ".join(str(s) for s in scores))
print(" + ".join(map(str, scores)))join() vs + for building strings
| Approach | Example | Best for | Watch out for |
|---|---|---|---|
join() | ", ".join(items) | Many pieces, any size | Items must already be strings |
+ operator | a + ", " + b | Two or three fixed pieces | Slow in long loops and needs str() for numbers |
| f-string | f"{a}, {b}" | A few values in a template | Not meant for variable-length lists |
print(*items, sep=", ") | print(*items, sep=", ") | Printing only | Does not return a string |
str(list) | str([1, 2]) | Debugging | Includes brackets and quotes |
Why join() is faster than repeated concatenation
Each + on strings creates a brand new string, so building a large result piece by piece can copy the same characters many times. join() first works out the final size and then fills it once. For a handful of pieces the difference is invisible, so choose whichever reads best; for hundreds of pieces, use join(). See Python f-strings for templates that mix a few values.
Examples you can run
Press Run, then edit the code and run it again.
Turn a list of words into a sentence
Choose a space as the separator.
words = ["Python", "makes", "text", "easy"]
sentence = " ".join(words)
print(sentence)Expected outputPython makes text easy
Join numbers after converting them
Numbers must become strings first.
prices = [4.5, 12, 3.25]
line = " | ".join(str(p) for p in prices)
print(line)Expected output4.5 | 12 | 3.25
Join the keys of a dictionary, and a set in sorted order
Joining a dict uses its keys. Sets have no fixed order, so sort first.
cart = {"pen": 2, "book": 1, "bag": 1}
print(", ".join(cart))
tags = {"b", "c", "a"}
print(",".join(sorted(tags)))Expected outputpen, book, bag
a,b,c
Common mistakes
Joining a list that contains numbers
✗ Wrong
nums = [1, 2, 3]
print(",".join(nums))✓ Right
nums = [1, 2, 3]
print(",".join(str(n) for n in nums))Convert each item to a string before joining.
Calling join() on the list
✗ Wrong
names = ["Asha", "Ravi"]
print(names.join(", "))✓ Right
names = ["Asha", "Ravi"]
print(", ".join(names))join() is a string method. Call it on the separator.
Passing the pieces as separate arguments
✗ Wrong
print("-".join("a", "b"))✓ Right
print("-".join(["a", "b"]))Wrap the pieces in a list or tuple.
Python String join() Method: questions people ask
What does join() do in Python?
It combines the strings of an iterable into one string, with the string you call it on placed between the items.
How do I join a list of integers into a string?
Convert the integers first, for example "".join(str(n) for n in nums) or "".join(map(str, nums)), because join() raises a TypeError for non-strings.
Why is join() called on the separator and not on the list?
Because join() belongs to str, so it works with any iterable (lists, tuples, generators) without each of those types needing its own method.
Keep learning
Step-by-step lessons:
Python StringsPython String MethodsPython Lists
Related:
Python String split() MethodHow to Convert a List to a String in PythonPython f-StringsPython String Cheat Sheet: Methods and Formatting
More in string methods:
Python String split() MethodPython String strip() MethodPython String lstrip() MethodPython String rstrip() MethodPython String replace() MethodPython String upper() MethodPython String lower() MethodPython String title() Method See all →