How to Join Strings in Python

How to Join Strings in Python

by Martin Breuss 0 Comments basics python

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.

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 Python

Test 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:

Python
>>> 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:

Python
>>> 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.

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:

Python
>>> 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:

Python
>>> 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.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

Locked learning resources

The full article is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

About Martin Breuss

Martin likes automation, goofy jokes, and snakes, all of which fit into the Python community. He enjoys learning and exploring and is up for talking about it, too. He writes and records content for Real Python and CodingNomads.

» More about Martin

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

What Do You Think?

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal.


Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!

Become a Member to join the conversation.

Keep Learning

Related Topics: basics python