You are currently viewing 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.

Advertisements

1. Syntax of join()

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


# Syntax of join()
string.join(iterable)

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)
print(str3)

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

# Output:
# A,B,C
# XABCYABCZ

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
myList=['Welcome','to','spark','by','examples']
print(myList)

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

# 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)
print(myString)

# 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
myList={'Welcome','to','spark','by','examples'}
print(myList)

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

# 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]
print(myAnohterList)

# 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'}
print(states)

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

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

Conclusion

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().