Open In App

Python String join() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Python join() is an inbuilt string function in Python used to join elements of the sequence separated by a string separator. This function joins elements of a sequence and makes it a string.

Python String join() Syntax

Syntax: string_name.join(iterable) 

Parameters:

  • Iterable – objects capable of returning their members one at a time. Some examples are List, Tuple, String, Dictionary, and Set

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

Type Error: If the iterable contains any non-string values, it raises a TypeError exception.

String join() in Python Example

In Python, we can use the join() method with different types of iterable such as Lists, Tuple, String, Dictionary, and Sets. Let’s understand them one by one with the help of examples.

Python3
str = '-'.join('hello')
print(str)

Output:

h-e-l-l-o

Join a List into a String in Python

Here, we have joined the list of elements using the join() method in two ways firstly joined all the elements in the list using an empty string as a separator and also join the elements of the list using “$” as a separator as seen in the output.

Python3
# Joining with empty separator
list1 = ['g', 'e', 'e', 'k', 's']
print("".join(list1))

# Joining with string
list1 = " geeks "
print("$".join(list1))

Output: 

geeks
$g$e$e$k$s$

Join a Tuple element into a String in Python

Here, we join the tuples of elements using the Python join() method in which we can put any character to join with a string.

Python3
# elements in tuples
list1 = ('1', '2', '3', '4')

# put any character to join
s = "-"

# joins elements of list1 by '-'
# and stores in string s
s = s.join(list1)

# join use to join a list of
# strings to a separator s
print(s)

Output: 

1-2-3-4

Join Sets element into a String using join() method

In this example, we are using a Python set to join the string.

Note: Set contains only unique value therefore out of two 4 one 4 is printed.

Python3
list1 = {'1', '2', '3', '4', '4'} 

# put any character to join
s = "-#-"

# joins elements of list1 by '-#-'
# and stores in string s
s = s.join(list1)

# join use to join a list of
# strings to a separator s
print(s)

Output: 

1-#-3-#-2-#-4

Joining String with a Dictionary using join()

When joining a string with a dictionary, it will join with the keys of a Python dictionary, not with values.

Python3
dic = {'Geek': 1, 'For': 2, 'Geeks': 3}

# Joining special character with dictionary
string = '_'.join(dic)

print(string)

Output:

'Geek_For_Geeks'

Note: When we join the dictionary keys it only joins the keys which are string only not an integer let’s see this in the code.

Python
dic = {1:'Geek', 2:'For', 3:'Geeks'}

# Joining special character with dictionary
string = '_'.join(dic)

print(string)

Output:

Hangup (SIGHUP)
Traceback (most recent call last):
  File "Solution.py", line 4, in <module>
    string = '_'.join(dic)
TypeError: sequence item 0: expected string, int found

Joining a list of Strings with a Custom Separator using Join()

In this example, we have given a separator which is separating the words in the list and we are printing the final result.

Python3
words = ["apple", "", "banana", "cherry", ""]
separator = "@ "
result = separator.join(word for word in words if word)
print(result) 

Output :

apple@ banana@ cherry


Last Updated : 14 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads