Python join() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

join() glues many strings into one string. You call it on the separator, and pass an iterable such as a list or a tuple: ", ".join(["a", "b"]) is "a, b". The separator goes only between the items, never at the ends.

It is the opposite of split(). Every item must be a string, or Python raises TypeError. Using an empty separator, "".join(items), simply glues the items together. The method returns a new string.

separator.join() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.

Syntax

Call it on the separator, and pass an iterable of strings.

python
text = separator.join(iterable)
CallWhat it does
", ".join(["a", "b", "c"])Returns 'a, b, c'. The separator goes between the items.
"-".join("abc")Returns 'a-b-c'. A string is an iterable of characters, so each character is joined.
",".join([1, 2])Raises TypeError. The items are numbers, not strings.

To break a string apart again use split(). To swap text inside a string use replace(), and to build text from values use format().

Library declarations
python
str.join(iterable) -> str
# returns: a new string
# raises: TypeError if an item is not a string

Parameters

ParameterDescription
iterableRequired. A list, tuple, set, generator, or any other iterable whose items are all strings. For a dictionary, the keys are joined.

Return Value

Return ValueDescription
strA new string with all the items and the separator between each pair. An empty iterable gives an empty string, and one item is returned as it is.

Python Examples

Example 1 — Join Words in Different Ways

Join the same list with a space, a hyphen, nothing, and a comma.

python
words = ["learn", "python", "today"]

print(" ".join(words))
print("-".join(words))
print("".join(words))
print(", ".join(words))

How It Works

  1. The separator sits between. A space puts one space between the words, and nothing before the first or after the last.
  2. Any text works. A hyphen or a comma with a space is just a different separator.
  3. An empty separator. "".join(words) glues the words with nothing between them.

Example 2 — Fix the TypeError With str()

Show the error that joining numbers causes, and two ways to convert them first.

python
numbers = [3, 14, 15, 92]

try:
    ", ".join(numbers)
except TypeError:
    print("join needs strings")

print(", ".join(map(str, numbers)))
print(", ".join([str(n) for n in numbers]))

How It Works

  1. Numbers fail. The items of numbers are integers, so join() raises TypeError, and the code prints a message instead.
  2. Convert with map(). map(str, numbers) turns every number into a string on the way.
  3. Or use a comprehension. A list comprehension with str(n) gives the same text.

Example 3 — Split and Join Together

Split text on whitespace and join it back with a single space, to remove extra spaces.

python
sentence = "  too   many    spaces  "
words = sentence.split()

print(words)
print(" ".join(words))
print(" ".join(sentence.split()) == "too many spaces")

How It Works

  1. Split first. split() drops the extra spaces and returns just the three words.
  2. Join with one space. " ".join(words) puts exactly one space between the words.
  3. A one-line cleaner. " ".join(sentence.split()) does both steps, and the result equals the clean text.

Frequently Asked Questions

It joins the strings of an iterable, such as a list or a tuple, into one string, with the string you call it on placed between the items.
Every item must be a string. If the list holds numbers, convert them first, for example ", ".join(map(str, numbers)).
Because it works with any iterable of strings, not only lists. The method lives on str, so the separator is the object you call it on.
Yes, for many items. join() builds the final string in one step, while += may copy the text again and again.
Use "\n".join(lines). It puts a newline between the lines, but not after the last one.

Next: ljust()

Return the string left-aligned in a field of a given width, padded on the right.

ljust() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful