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 and delimiter

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. In the last two statements, we used the comma and – as the separator between the string tuple.

text = ('a', 'b', 'c')

x = ' '.join(text)
print(x)
 
seq = ','
print(seq.join(text))
 
seq = '-'
print(seq.join(text))
a b c
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 combine the List items. Here, we used the \n (newline) as the separator between the list items. I suggest you refer to the List article in Python.

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

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

liVal = ['t', 'u', 't', 'o', 'r', 'i','a', 'l']
print(liVal)
print()
 
seq = ''
print(seq.join(liVal))
print()
 
seq = '$$$'
print(seq.join(liVal))
print()
 
seq = '->'
print(seq.join(liVal))
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.

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

youGuysHowHiare

you->Guys->How->Hi->are

you###Guys###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.

dictItems = {'name': 'Steve', 'Job': 'Developer', 'Country': 'USA'}
 
seq = '$$$'
print(seq.join(dictItems.keys()))
print()
 
seq = '$$$'
print(seq.join(dictItems.values()))
print()
 
seq = '@#$'
print(seq.join(dictItems.values()))
print()
 
seq = '@#$'
print(seq.join(dictItems.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 = 'United'
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']))
HUnitedEUnitedLUnitedLUnitedO

HUnitedEUnitedLUnitedLUnitedO

TUnitedUUnitedTUnitedOUnitedRUnitedIUnitedAUnitedL

TUnitedUUnitedTUnitedOUnitedRUnitedIUnitedAUnitedL

AUnitedBUnitedCUnitedD

AUnitedBUnitedCUnitedD

As you can see from the above screenshot, both the print(seq.join(‘HELLO’)) and ([‘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.

name = 'tutorialgateway'
print(name)
print()
 
seq = ','
print(seq.join(name))
print()
 
lang = 'Computers'
print(lang)
print()
 
seq = ','
print(seq.join(lang))
tutorialgateway

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

Computers

C,o,m,p,u,t,e,r,s

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 combine the list items. As you can see, it as throwing an error: TypeError: sequence item 3: expected str instance, int found

listvalues = ['UK', 'INDIA', 'USA', 1, 'FRANCE']
print(listvalues)
print()
 
seq = '$$$'
print(seq.join(listvalues))
['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.

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

Hi 20 Hello 40 World 140 From 3.0 Now

[email protected]@[email protected]@[email protected]@[email protected]@Now

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

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.

listTxt = ['Hi', 20, 'Hello', 40, 'World', 140, 'From', 3.0, 'Now']
print(listTxt)
print()
 
sequence = ' '
strAfterJn = sequence.join(map(str, listTxt))
print(strAfterJn)
print()
 
sequence = ', '
strAfterJn = sequence.join(map(str, listTxt))
print(strAfterJn)
print()
 
sequence = ' *#* '
strAfterJn = sequence.join(map(str, listTxt))
print(strAfterJn)
['Hi', 20, 'Hello', 40, 'World', 140, 'From', 3.0, 'Now']

Hi 20 Hello 40 World 140 From 3.0 Now

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

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

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

listVal = [ 20, 40, 140, 'Learn', 3.0]
print(listVal)
print()
 
seq = ' '
afterJn  = seq.join('{0}'.format(item) for item in listVal)
print(afterJn )
print()
 
seq = ', '
afterJoin = seq.join("'{0}'".format(item) for item in listVal)
print(afterJoin)
print()
 
seq = ' $ '
afterJn  = seq.join('{0}'.format(item) for item in listVal)
print(afterJn )
print()
 
seq = ' *+* '
afterJn  = seq.join('{0}'.format(item) for item in listVal)
print(afterJn )
[20, 40, 140, 'Learn', 3.0]

20 40 140 Learn 3.0

'20', '40', '140', 'Learn', '3.0'

20 $ 40 $ 140 $ Learn $ 3.0

20 *+* 40 *+* 140 *+* Learn *+* 3.0