What is the String join() method in Python?

The String join() method combines all of the items in an iterable into a single string with each item joined by the string separator.

Syntax


string.join(iterable)

Parameter

The join() method requires an iterable object where all the returned values are strings.

Return value

The join() method returns a string concatenated with the elements of the iterable.

Code

The following code shows how to use the String join() method:

Example 1

A program that joins all items in a dictionary into a string, using "-" as the separator.

# Creating a dictionary
myDict = {"name": "Maria", "id": "1128de0012", "gender": "Female"}
# Using "-" as seperator
mySeparator = "-"
new_dict = mySeparator.join(myDict)
print(new_dict)

Example 2

A program that joins all items in a list into a string, using "OF" as the separator.

myList = ['1','2','3','4']
mySeperator = "OF"
# joins elements of myList by 'OF'
# and print
print(mySeperator.join(myList))

Example 3

A program that joins with an empty string.

# using of join() to join a list
# without any separator.
myList = ['E','d','u','c', 'a', 't', 'i', 'v', 'e']
# Joining with empty separator
print("".join(myList))