Python String join() Method
Python join() is an inbuilt string function used to join elements of a sequence separated by a string separator. This function joins elements of a sequence and makes it a string.
Python String join() Syntax
Syntax: separator_string.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.
# This will join the characters of the string 'hello' with '-'
str = '-'.join('hello')
print(str) # Output: h-e-l-l-o
Output:
h-e-l-l-oJoin 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.
# 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.
# 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-4Join 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.
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-#-4Joining 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.
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.
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.
words = ["apple", "", "banana", "cherry", ""]
separator = "@ "
result = separator.join(word for word in words if word)
print(result)
Output :
apple@ banana@ cherryPython String Join() Method – FAQs
Can we use the join() method with a tuple of strings?
Yes, you can use the
join()method with a tuple of strings. Thejoin()method concatenates the elements of the tuple into a single string, using the specified separator.Example:
tuple_of_strings = ('Hello', 'World')
result = ' '.join(tuple_of_strings)
print(result) # Output: "Hello World"
What happens if we use the join() method with non-string elements?
If you use the
join()method with non-string elements, it will raise aTypeErrorbecausejoin()expects an iterable of strings. You need to convert non-string elements to strings first.Example:
elements = [1, 2, 3]
# This will raise a TypeError
# result = '-'.join(elements)
# Convert elements to strings
result = '-'.join(map(str, elements))
print(result) # Output: "1-2-3"
How to join elements of a list with a space as a separator?
To join elements of a list with a space as a separator, use the
join()method with a space character' '.Example:
list_of_strings = ['Hello', 'World']
result = ' '.join(list_of_strings)
print(result) # Output: "Hello World"
How to join elements of a list with a comma as a separator?
To join elements of a list with a comma as a separator, use the
join()method with a comma character','.Example:
list_of_strings = ['apple', 'banana', 'cherry']
result = ','.join(list_of_strings)
print(result) # Output: "apple,banana,cherry"
Can the join() method be used with sets?
Yes, the
join()method can be used with sets, but the elements must be strings. Note that sets are unordered, so the order of the elements in the resulting string may vary.Example:
set_of_strings = {'apple', 'banana', 'cherry'}
result = ','.join(set_of_strings)
print(result) # Output: "banana,apple,cherry" (order may vary)


