Home / Python / Reference / String methods

Python string join() method

Glue a list of strings together, with a separator between them.

Syntax

sep.join(iterable)

Called on the separator, not on the list. Every item must be a string: convert numbers first.

Parameters

iterablestrings to join

Return value

str

Example

Code

words = ["tea", "milk", "sugar"]
print(", ".join(words))
print("".join(["P", "y"]))
print("-".join("abc"))
nums = [1, 2, 3]
print(" + ".join(str(n) for n in nums), "=", sum(nums))

Output

tea, milk, sugar
Py
a-b-c
1 + 2 + 3 = 6
Good to know. ", ".join([1, 2]) is a TypeError: convert numbers with str() first.

Related

Learn it step by step in the free lesson Strings, with exercises that check your answers.