Python’s built-in string method .join()
lets you combine string elements from an iterable into a single string, using a separator that you specify. You call .join()
on the separator, passing the iterable of strings to join.
By the end of this tutorial, you’ll understand that:
- You use
.join()
in Python to combine string elements with a specified separator. - A separator is the piece of text you want inserted between each substring.
- To join list elements, you call
.join()
on a separator string, passing the list as the argument. .join()
inserts the separator between each list element to form a single string.- The
.join()
method returns a new string that is the concatenation of the elements in the iterable, separated by the specified string. - For smaller string concatenation tasks, you can use the concatenation operator (
+
) or f-strings instead of.join()
.
Python’s built-in str.join()
method gives you a quick and reliable way to combine multiple strings into a single string. Whether you need to format output or assemble data for storage, .join()
provides a clean and efficient approach for joining strings from an iterable.
In the upcoming sections, you’ll learn the basic usage of .join()
to concatenate strings effectively. You’ll then apply that knowledge to real-world scenarios, from building CSV files to constructing custom log outputs. You’ll also discover some surprising pitfalls and learn how to avoid them.
Get Your Code: Click here to download the free sample code that shows you how to join strings in Python.
Take the Quiz: Test your knowledge with our interactive “How to Join Strings in Python” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
How to Join Strings in PythonTest your understanding of Python's .join() string method for combining strings, handling edge cases, and optimizing performance.
How to Join Strings in Python Using .join()
To use the string method .join()
, you call .join()
on a separator string and pass an iterable of other strings as the argument. The method returns a single string, where it has inserted the separator string between each element of the iterable:
>>> words = ["Time", "flies", "like", "an", "arrow!"]
>>> " ".join(words)
'Time flies like an arrow!'
In this example, you joined a list of words into one sentence, separated by spaces.
At first glance, this usage might look a little backward. In many other string operations, you call the method on the main string that you want to manipulate. However, with .join()
, you call the method on the separator string, then pass the iterable of strings that you want to combine:
>>> separator = " "
>>> separator.join(words)
'Time flies like an arrow!'
This example achieves the same result as the earlier one but splits the process into two steps. Defining separator
separately makes the code more readable and avoids the potentially odd-looking syntax of calling .join()
directly on a short string literal.
Note: Remember that .join()
is a string method, which means that you’ll need to call it on a single string object. Keeping that in mind may help you remember why you need to call it on the separator string.
You rarely see code that’s written in multiple steps where you assign the separator string to a variable, like you did in the example above.
In typical usage, you call .join()
directly on the separator string, all in one line. This approach is more concise and highlights that any valid string can be your separator, whether it’s whitespace, a dash, or a multicharacter substring.
Join With an Empty String
What if you don’t want any separator at all, but just want to concatenate the items? One valid approach is to use an empty string (""
) as the separator:
>>> letters = ["A", "B", "C", "D"]
>>> "".join(letters)
'ABCD'
This code snippet concatenates the letters in the list, forming a single string "ABCD"
. Using an empty string as the separator is a great way to assemble strings without a delimiter between them.
Combine Strings of Characters
Since .join()
can take any iterable of strings—not just lists—you can even pass a string as an argument. Because strings are iterable, Python iterates over each character in that string, considering each character as a separate element:
>>> characters = "ABCD"
>>> ",".join(characters)
'A,B,C,D'
By calling .join()
on ","
and passing the string characters
, you effectively place a comma between every single character in "ABCD"
. This might not always be what you intend, but it’s a neat trick to keep in mind if you ever need to treat each character as a separate element.