Python string join()

In this article, we will take a look at python string join() function, its use with example programs.
join() function is used to concatenate strings together. The string values to be joined may be supplied as a list, set, tuple or dictionary keys.

Syntax

Python string join() function accepts an iterable of string values as argument or parameter.
Iterable means a structure that can be iterated, such as a list, set, tuple,  dictionary or a string.
join() is invoked on a string and this string is inserted as a separator between the iterable elements.
Its syntax is

'-'.join(<iterable>)

Here, ‘-‘ will be inserted as a separator between the elements of the iterable.

Return value

Return value of join() is a string made by concatenating the elements of the iterable with the separator inserted between them.
Remember that iterable whose elements are concatenated should be of string type, otherwise a TypeError will be raised.

Python docs for join() state

Concatenate any number of strings.

The string whose method is called is inserted in between each given string.
The result is returned as a new string.

Python join() function with list

Below is an example of join() function with list as parameter and ‘ ‘(space) as separator.

# define a list
l=['learning', 'python','is','fun']
result = ' '.join(l)
print('Joining list elements:',result)

Output is

Joining list elements: learning python is fun

Notice how join() inserts the string between list elements.
As you can guess, join() can be used to convert list to a string in python.

Python join() function with set

Below is an example of join() function with set parameter and ‘-‘(hyphen) as separator.

# define a set
s={'learning', 'python','is','fun'}
result = '-'.join(s)
print('Joining set elements:',result)

Output is

Joining set elements: learning-python-is-fun

Python join() function with tuple

Example of join() function to concatenate elements of a tuple with ‘,’(comma) as separator is given below

# define a tuple
t=('learning', 'python','is','fun')
result = '.'.join(t)
print('Joining tuple elements:',result)

Output is

Joining tuple elements: learning-python-is-fun

Python join() function with dictionary

A python dictionary is a collection of key-value pairs.
When join() is called with a dictionary as parameter, it combines the keys of the dictionary with the string separator. Example,

d={'A':1, 'B':2, 'C':3}
result = '+'.join(d)
print('Joining dictionary keys:',result)

The keys of dictionary will be combined with ‘+’(plus) as separator.
Output will be

Joining dictionary keys: A+B+C

Python join() function with string

A python string is also an iterable, with the characters of the string as elements.
So, when a string is provided to join() as a parameter, characters of the string are combined with the separator. Example,

result = ' > '.join('codippa')
print('Joining string characters:',result)

Output is

Joining string characters: c > o > d > i > p > p > a

TypeError in join()

As stated above, the values of iterable argument supplied to join() function should be of string types. If the argument is a dictionary, then its keys should be strings.
If any one element is not of string type, join() throws a TypeError. Thus, all the below examples will throw TypeError.

#. list
'-'.join(['A', 'B', 1, 'C'])
# tuple
'-'.join((0, 1, 'Hi'))

# set
' '.join({'one', 1, 'two', 2 })

# dictionary
'-'.join({1:'A', 2:'B' })

That is all on Python join() function and its usage to combine strings with list, set, tuple and dictionary.
Hope the article was useful.