Python String join

The Python join method is used to join or concatenate a string with iterable. This Python string join function accepts iterable as an argument. The python string join function attaches a given separator while joining the strings in an iterable.

In this section, we discuss how to use this Python join function to join strings in a List, Dictionary, Set, etc., with an example of each. The syntax of this string join in Python is

separator.join(iterable)

Python join strings with spaces

It is a simple example to show you the Python string join function. Here, we declared a tuple of characters a, b and c. Next, we used the space as the separator and the tuple as an argument of the Python string join function. It joins the tuple items separated by space.

text = ('a', 'b', 'c')
 
x = ' '.join(text)
print(x)

Python string join output

a b c

Python join with Delimiter

It is another example of the join function. Here, we used the comma and – as the separator between the string tuple.

text = ('a', 'b', 'c')
 
seq = ','
print(seq.join(text))
 
seq = '-'
print(seq.join(text))
a,b,c
a-b-c

This time we are using the tuple of string words. Next, we used # and $ as the separators for Python string join function.

text = ('Hi', 'Hello', 'World')
 
seq = '#'
print(seq.join(text))
print()
 
seq = '$'
print(seq.join(text))
Hi#Hello#World

Hi$Hello$World

Python join List elements

We are using the String Join function to join the List items. Here, we used the \n (newline) as the separator between the list items. I suggest you refer to the Python List in Python.

list_text = ['Hi', 'Hello', 'World', 'From', 'Python']
 
seq = '\n'
print(seq.join(list_text))

Output

Hi
Hello
World
From
Python

It is another example to join the Python List items. Here, we used different separators ‘’, $$$, -> to join the list items.

list_values = ['t', 'u', 't', 'o', 'r', 'i','a', 'l']
print(list_values)
print()
 
seq = ''
print(seq.join(list_values))
print()
 
seq = '$$$'
print(seq.join(list_values))
print()
 
seq = '->'
print(seq.join(list_values))
print()
['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']

tutorial

t$$$u$$$t$$$o$$$r$$$i$$$a$$$l

t->u->t->o->r->i->a->l

>>> 

Python join Set elements

Here, we are using the join function for joining the set items. I suggest you to refer to Python Set.

set_values = {'Hi', 'Python', 'How', 'are', 'you'}
print(set_values)
print()
 
seq = ''
print(seq.join(set_values))
print()
 
seq = '->'
print(seq.join(set_values))
print()
 
seq = '###'
str1 = seq.join(set_values)
print(str1)
{'you', 'Python', 'How', 'Hi', 'are'}

youPythonHowHiare

you->Python->How->Hi->are

you###Python###How###Hi###are

Python join Dictionary items

In this String method example, we are using the join function to Join the dictionary items. Refer to Python Dictionary.

dict_values = {'name': 'Steve', 'age': 25, 'Country': 'USA'}
 
seq = '@'
print(seq.join(dict_values))
print()
 
seq = '$'
print(seq.join(dict_values))
Python Join 7

As you can see from the above screenshot, join function joined the dictionary keys. It is the default option available in Python. However, you can use the dictionary function values to join the dictionary values.

dict_values = {'name': 'Steve', 'Job': 'Developer', 'Country': 'USA'}
 
seq = '$$$'
print(seq.join(dict_values.keys()))
print()
 
seq = '$$$'
print(seq.join(dict_values.values()))
print()
 
seq = '@#$'
print(seq.join(dict_values.values()))
print()
 
seq = '@#$'
print(seq.join(dict_values.values()))
name$$$Job$$$Country

Steve$$$Developer$$$USA

[email protected]#[email protected]#$USA

[email protected]#[email protected]#$USA

Python string join example

Until now, we are only using the iterable inside the join function (tuple, list, set, and dictionary). Here, we are using the string as an argument of join function. 

seq = 'python'
print(seq.join('HELLO'))
print()
 
print(seq.join(['H','E', 'L', 'L', 'O']))
print()
 
print(seq.join('TUTORIAL'))
print()
 
print(seq.join(['T', 'U', 'T', 'O', 'R', 'I', 'A', 'L']))
print()
 
print(seq.join('ABCD'))
print()
 
print(seq.join(['A', 'B', 'C', 'D']))
HpythonEpythonLpythonLpythonO

HpythonEpythonLpythonLpythonO

TpythonUpythonTpythonOpythonRpythonIpythonApythonL

TpythonUpythonTpythonOpythonRpythonIpythonApythonL

ApythonBpythonCpythonD

ApythonBpythonCpythonD

As you can see from the above screenshot, both the print(seq.join(‘HELLO’)) and print(seq.join([‘H’,’E’, ‘L’, ‘L’, ‘O’])) are returning the same result. This is because, internally, seq.join(‘HELLO’) will be converted to list of characters.

It is another example of a Python string join to understand the implicit conversion. Here, we used different separators to separate tutorialgateway string.

# Python split String using Join
name = 'tutorialgateway'
print(name)
print()
 
seq = ','
print(seq.join(name))
print()
 
lang = 'Python'
print(lang)
print()
 
seq = ','
print(seq.join(lang))
tutorialgateway

t,u,t,o,r,i,a,l,g,a,t,e,w,a,y

Python

P,y,t,h,o,n

Python join string and int

So far, we are working with string data. In this example, we declared a mixed list of string and integer. Next, we used the Python string join function with $$$ as a separator to join the list items. As you can see, it as throwing an error: TypeError: sequence item 3: expected str instance, int found

list_values = ['UK', 'INDIA', 'USA', 1, 'FRANCE']
print(list_values)
print()
 
seq = '$$$'
print(seq.join(list_values))
['UK', 'INDIA', 'USA', 1, 'FRANCE']

Traceback (most recent call last):
  File "/Users/suresh/Desktop/simple.py", line 6, in <module>
    print(seq.join(list_values))
TypeError: sequence item 3: expected str instance, int found

This time, within the Python join function, we used the For Loop to iterate each item in a List. Next, we used the str function to convert the List item to a string.  

list_values = ['Hi', 20, 'Hello', 40, 'World', 140, 'From', 3.0, 'Python']
print(list_values)
print()
 
seq = ' '
str_after_join = seq.join(str(item) for item in list_values)
print(str_after_join)
print()
 
seq = '@'
str_after_join = seq.join(str(item) for item in list_values)
print(str_after_join)
print()
 
seq = ' *+* '
str_after_join = seq.join(str(item) for item in list_values)
print(str_after_join)
['Hi', 20, 'Hello', 40, 'World', 140, 'From', 3.0, 'Python']

Hi 20 Hello 40 World 140 From 3.0 Python

[email protected]@[email protected]@[email protected]@[email protected]@Python

Hi *+* 20 *+* Hello *+* 40 *+* World *+* 140 *+* From *+* 3.0 *+* Python

There is an alternative approach to work with mixed lists. Here, we used the map function along with Python string join to achieve the same result. Refer to the Python map function.

list_text = ['Hi', 20, 'Hello', 40, 'World', 140, 'From', 3.0, 'Python']
print(list_text)
print()
 
sequence = ' '
str_after_join = sequence.join(map(str, list_text))
print(str_after_join)
print()
 
sequence = ', '
str_after_join = sequence.join(map(str, list_text))
print(str_after_join)
print()
 
sequence = ' *#* '
str_after_join = sequence.join(map(str, list_text))
print(str_after_join)
['Hi', 20, 'Hello', 40, 'World', 140, 'From', 3.0, 'Python']

Hi 20 Hello 40 World 140 From 3.0 Python

Hi, 20, Hello, 40, World, 140, From, 3.0, Python

Hi *#* 20 *#* Hello *#* 40 *#* World *#* 140 *#* From *#* 3.0 *#* Python

It is another approach of joining string and int in Python. Here, we are using the format function inside the for loop to format list items to strings.

list_values = [ 20, 40, 140, 'Python', 3.0]
print(list_values)
print()
 
seq = ' '
after_join = seq.join('{0}'.format(item) for item in list_values)
print(after_join)
print()
 
seq = ', '
after_join = seq.join("'{0}'".format(item) for item in list_values)
print(after_join)
print()
 
seq = ' $ '
after_join = seq.join('{0}'.format(item) for item in list_values)
print(after_join)
print()
 
seq = ' *+* '
after_join = seq.join('{0}'.format(item) for item in list_values)
print(after_join)
[20, 40, 140, 'Python', 3.0]

20 40 140 Python 3.0

'20', '40', '140', 'Python', '3.0'

20 $ 40 $ 140 $ Python $ 3.0

20 *+* 40 *+* 140 *+* Python *+* 3.0