Python 3 reference · String Methods

Python String join() Method

Join the strings of an iterable into one string, with this string as the separator between them.

Python 3 Since 3.0

Syntax

str.join(iterable)

Parameters

ParameterDescription
iterable Strings to join; any non-string item raises TypeError.

Return value

A new string.

Examples

Join with a separatorTry it yourself
print(", ".join(["red", "green", "blue"]))
print("".join(["a", "b", "c"]))
print("-".join("abc"))

Output

red, green, blue
abc
a-b-c
Join numbers (convert first)Try it yourself
numbers = [1, 2, 3]
print(" + ".join(str(n) for n in numbers), "=", sum(numbers))

Output

1 + 2 + 3 = 6
Build multi-line textTry it yourself
lines = ["first line", "second line"]
print("\n".join(lines))

Output

first line
second line

Common mistakes

  • The separator comes first: ", ".join(items), not items.join(", ").
  • All items must be strings: convert with str() or map(str, items).
  • Building text with += in a loop is slow for long text; collect parts in a list and join() once.

Version history

VersionChange
Python 3.0Added