The String join() method allows you to concatenate elements of an iterable (such as a list, tuple, or set) into a single string, with each element separated by a specified separator.
Syntax
my_string.join(iterable)join() Parameters
Python String join() method parameters:
| Parameter | Condition | Description |
|---|---|---|
iterable |
Required | Any iterable (like list, tuple, set, dictionary etc.) whose items are strings |
join() Return Value
Python String join() method returns the string obtained by concatenating the items of the given iterable and the my_string as separator between elements of the iterable.
Examples
Example 1: Basic example of Joining Elements of an Iterable with join()
The join() method returns the string obtained by concatenating the items of the given iterable, using the string on which you are invoking the method as separator between elements.
For example, let's join all items in a list with comma
my_list = ['Tom', 'Anna', 'David']
result = ','.join(my_list)
print(result) # Output: Tom,Anna,Davidoutput
Tom,Anna,DavidBut you can use any string you want as separator:
my_list = ['Tom', 'Anna', 'David']
result1 = '-'.join(my_list)
result2 = ' '.join(my_list)
result3 = ' # '.join(my_list)
print(result1) # Output: Tom-Anna-David
print(result2) # Output: Tom Anna David
print(result3) # Output: Tom # Anna # Davidoutput
Tom-Anna-David
Tom Anna David
Tom # Anna # DavidExample 2: Join Elements of an Iterable of Size 1 with join()
The join() method is smart enough to insert the delimiter in between the strings rather than just adding at the end of every string. So, if you pass an iterable of size 1, you will not see the delimiter.
For example, if you have a list with a single string element and you use the join() method with a delimiter, the result will be the original string without the delimiter:
single_element = ['Hello']
delimiter = ','
result = delimiter.join(single_element)
print(result) # Output: Hellooutput
HelloExample 3: Join Elements of an Iterable with Empty Separator
An empty string can be used as separator, effectively concatenating the elements without any space between them.
my_list = ['Tom', 'Anna', 'David']
result = ''.join(my_list)
print(result) # Output: TomAnnaDavidoutput
TomAnnaDavidExample 4: Join Non-String Elements of an Iterable
Note that a TypeError will be raised if there are any non-string values in iterable!
my_list = [1, 2, 3, 4, 5, 6]
result = ','.join(my_list) # Raises TypeError: sequence item 0: expected string, int found
print(result) output
Traceback (most recent call last):
File "main.py", line 2, in <module>
result = ','.join(my_list)
TypeError: sequence item 0: expected str instance, int foundTo avoid this exception, you need to convert each item in a list to string. You can use List Comprehension.
my_list = [1, 2, 3, 4, 5, 6]
result = ','.join(str(val) for val in my_list)
print(result) # Output: 1,2,3,4,5,6output
1,2,3,4,5,6Example 4: Join Elements of a List with join()
You can give to the join() method any iterable you want, for example a List.
my_list = ['Tom', 'Anna', 'David']
result = ', '.join(my_list)
print(result) # Output: Tom, Anna, Davidoutput
Tom, Anna, DavidExample 5: Join Elements of a Tuple with join()
You can also use a Tuple as iterable in the join() method:
my_tuple = ('Tom', 'Anna', 'David')
result = ', '.join(my_tuple)
print(result) # Output: Tom, Anna, Davidoutput
Tom, Anna, DavidExample 6: Join Elements of a Dictionary with join()
When you use a dictionary as an iterable, all dictionary keys are joined by default.
my_dict = {'name':'Tom', 'city':'New York'}
result = ','.join(my_dict)
print(result) # Output: name,cityoutput
name,cityTo join all values, call values() method on dictionary and pass it as an iterable.
my_dict = {'name':'Tom', 'city':'New York'}
result = ','.join(my_dict.values())
print(result) # Output: Tom,New Yorkoutput
Tom,New YorkTo join all keys and values, use join() method with List Comprehension.
my_dict = {'name':'Tom', 'city':'New York'}
result = ','.join('='.join((key,val)) for (key,val) in my_dict.items())
print(result) # Output: name=Tom,city=New Yorkoutput
name=Tom,city=New YorkExample 7: Join Elements of a Set with join()
A Set can be used as iterable in the join() method:
my_set = {'Tom', 'Anna', 'David'}
result = ', '.join(my_set)
print(result) # Output: David, Anna, Tomoutput
David, Anna, TomExample 8: Join Elements of a String with join()
A String is also an iterable that can be used in the join() method: in this case, the elements are the individual characters that make up the string.
my_string = 'Tutorial Reference'
result = ', '.join(my_string)
print(result) # Output: T, u, t, o, r, i, a, l, , R, e, f, e, r, e, n, c, eoutput
T, u, t, o, r, i, a, l, , R, e, f, e, r, e, n, c, ejoin() method vs Concatenation Operator +
Concatenation operator + is good solution to join two strings. But if you need to join more strings, it is convenient to use join() method.
For example, there is not much difference with only two strings to concatenate:
x = 'aaa' + 'bbb'
print(x) # Output: aaabbb
x = ''.join(['aaa','bbb'])
print(x) # Output: aaabbboutput
aaabbb
aaabbbBut, if you have more strings, it becomes easier and clearer to use concatenation with join() method:
x = 'aaa' + 'bbb' + 'ccc' + 'ddd' + 'eee'
print(x) # Output: aaabbbcccdddeee
x = ''.join(['aaa','bbb','ccc','ddd','eee'])
print(x) # Output: aaabbbcccdddeeeoutput
aaabbbcccdddeee
aaabbbcccdddeee