Python String join() Explained

The Python str.join() method is used to join elements or values from a sequence object (list, set, tuple e.t.c) into a string by comma, space, or any custom separator. This method is called on the separator string and passed the sequence of strings as an argument and returns a string after joining.

1. Syntax of join()

Following is the syntax of the string join() method.

# Syntax of join()

1.1 Parameters of join()

  • The join() method takes the iterable objects as an argument, these include List, Tuple, String, Dictionary, and Set.
  • The type of iterable should hold string values. Using it on an integer will return an error.

2. Join String

When you join a string with a separator in python, it separates each character in a string by the specified delimiter and returns a new string. Here is an example.

# Create two strings
str1 = "ABC"
str2 = "XYZ"

# Join string with character
str3 = ",".join(str1)

# Join string with another string
# Join strings
str3 = str1.join(str2)

# Output:
# A,B,C

3. Join List of Strings

Using the list of strings as an argument to the python str.join() method, you can join the list of strings into a single string by separating each string by the specified separator. The below example uses the space separator to just list of string values. The join() is most commonly used to convert a list to a string.

Note that you should have a list with strings, using join() on a list with numbers returns an error.

# Create list to strings

# Join list by space separator
myString = " ".join(myList)

# Output:
# ['Welcome', 'to', 'spark', 'by', 'examples']
# Welcome to spark by examples

In this example, the join() method is called on the string ” ” (a space), and the list ['Welcome','to','spark','by','examples'] is passed as an argument. This joins the strings in the list, using the space as a separator, to create the final string “Welcome to spark by examples”.

Let’s see another example of joining the list of strings by a comma separator in Python.

# Join list by comma separator
myString = ",".join(myList)

# Output:
# ['Welcome', 'to', 'spark', 'by', 'examples']
# Welcome,to,spark,by,examples

4. Join a Set of Strings

Similarly, to join a set of strings in python use the set as an argument to the join() method. The below example uses the space separator to convert a set to a string. Note that you should have a set with strings, using join() on a set with numbers also returns an error.

# Create set of strings

# Use join() with set of strings
myString = " ".join(myList)

# Output:
# {'examples', 'to', 'spark', 'Welcome', 'by'}
# examples to spark Welcome by

5. Joining Numbers into String

When you use join() with an iterable object, you need to have a list with the string type, in case you have numbers and you wanted to join, use the map() along with the join().

myAnohterList = [10,20,30,40]

# Use join() with map()
print(" ".join(map(str, myAnohterList)))

# Output:
# [10, 20, 30, 40]
# 10 20 30 40

6. Join Dictionaries

Finally, let’s join the keys of the dictionaries into a single string by a comma separator. You can’t use this to merge dictionaries.

# Create dictionary
states = {'CA': 'California', 'NY': 'New York'}

# Join only keys
sep = ','
str2 = sep.join(states)

# Output:
# {'CA': 'California', 'NY': 'New York'}


In this article, you have learned the Python str.join() method that is used to join a sequence of strings into a single string by comma, space, or any custom separator. This method can be used only on with sequence of strings, if you wanted to join a sequence of integers into a string, use map().


I am a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, I have honed my expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. My journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. I have started this to share my experiences with the data as I come across. You can learn more about me at LinkedIn

Leave a Reply

You are currently viewing Python String join() Explained