Skip to main content

Python String join() Function

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.

note

The string on which you invoke the join() method is the separator between elements that it concatenates.

Syntax

my_string.join(iterable)

join() Parameters

Python String join() function parameters:

ParameterConditionDescription
iterableRequiredAny iterable (like list, tuple, set, dictionary etc.) whose items are strings

join() Return Value

Python String join() function returns the string obtained by concatenating the items of the given iterable and the my_string as separator between elements of the iterable.

danger

A TypeError exception will be raised if there are any non-string values in iterable. So, you must be sure that values that you are joining are strings!

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,David

output

Tom,Anna,David

But 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 # David

output

Tom-Anna-David
Tom Anna David
Tom # Anna # David

Example 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: Hello

output

Hello

Example 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: TomAnnaDavid

output

TomAnnaDavid

Example 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 found

To 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,6

output

1,2,3,4,5,6

Example 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, David

output

Tom, Anna, David

Example 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, David

output

Tom, Anna, David

Example 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,city

output

name,city

To 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 York

output

Tom,New York

To 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 York

output

name=Tom,city=New York

Example 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, Tom

output

David, Anna, Tom
note

A set is an unordered collection of items, so you may get different output since the order is random.

Example 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, e

output

T, u, t, o, r, i, a, l,  , R, e, f, e, r, e, n, c, e

join() 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:

# concatenation operator
x = 'aaa' + 'bbb'
print(x) # Output: aaabbb

# join() method
x = ''.join(['aaa','bbb'])
print(x) # Output: aaabbb

output

aaabbb
aaabbb

But, if you have more strings, it becomes easier and clearer to use concatenation with join() method:

# concatenation operator
x = 'aaa' + 'bbb' + 'ccc' + 'ddd' + 'eee'
print(x) # Output: aaabbbcccdddeee

# join() method
x = ''.join(['aaa','bbb','ccc','ddd','eee'])
print(x) # Output: aaabbbcccdddeee

output

aaabbbcccdddeee
aaabbbcccdddeee