Python String join() Method

Joins all items in an iterable into a single string

Usage

The join() method joins all items in an iterable into a single string.

Call this method on a string you want to use as a delimiter like comma, space etc.

If there are any non-string values in iterable, a TypeError will be raised.

Syntax

string.join(iterable)

Python string join() method parameters
ParameterConditionDescription
iterableRequiredAny iterable (like list, tuple, dictionary etc.) whose items are strings

Return Value

The method returns the string obtained by concatenating the items of an iterable.

Basic Examples

Example: Join all items in a list with comma

L = ['red', 'green', 'blue']
x = ','.join(L)
print(x)    # red,green,blue

Example: Join list items with space

L = ['The', 'World', 'is', 'Beautiful']
x = ' '.join(L)
print(x)    # The World is Beautiful

Example: Join list items with newline

L = ['First Line', 'Second Line']
x = '\n'.join(L)
print(x)
# First Line
# Second Line

A delimiter can contain multiple characters.

Example: Join with delimiter containing multiple characters

L = ['the beginning', 'the end', 'the beginning']
x = ' is '.join(L)
print(x)    # the beginning is the end is the beginning

join() on Iterable of Size 1

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 won’t see the delimiter.

Example:

L = ['red']
x = ','.join(L)
print(x)    # red

Join a List of Integers

If there are any non-string values in iterable, a TypeError will be raised.

Example:

# TypeError: sequence item 0: expected string, int found
L = [1, 2, 3, 4, 5, 6]
x = ','.join(L)
print(x)

To avoid such exception, you need to convert each item in a list to string.

The list comprehension makes this especially convenient.

Example: Join list of integers with list comprehension

L = [1, 2, 3, 4, 5, 6]
x = ','.join(str(val) for val in L)
print(x)    # 1,2,3,4,5,6

join() on Dictionary

When you use a dictionary as an iterable, all dictionary keys are joined by default.

Example: Join all dictionary keys

L = {'name':'Bob', 'city':'seattle'}
x = ','.join(L)
print(x)    # city,name

To join all values, call values() method on dictionary and pass it as an iterable.

Example: Join all dictionary values

L = {'name':'Bob', 'city':'seattle'}
x = ','.join(L.values())
print(x)    # seattle,Bob

To join all keys and values, use join() method with list comprehension.

Example: Join all dictionary keys and values

L = {'name':'Bob', 'city':'seattle'}
x = ','.join('='.join((key,val)) for (key,val) in L.items())
print(x)    # city=seattle,name=Bob

join() vs Concatenation operator +

Concatenation operator + is perfectly fine solution to join two strings.

But if you need to join more strings, it is convenient to use join() method.

Example:

# concatenation operator
x = 'aaa' + 'bbb'
print(x)    # aaabbb
# join() method
x = ''.join(['aaa','bbb'])
print(x)    # aaabbb