Joins a list of strings into one string.
Synopsis
separator.join(list)
Description
The string before the dot goes between the items. join is the opposite of split.
Parameters
| Name | Meaning |
|---|---|
list |
A list of strings. |
Return value
A str.
Example
colours = ["red", "green", "blue"]
print(", ".join(colours))
print("".join(["a", "b", "c"]))
print("-".join("abc"))
It prints:
red, green, blue
abc
a-b-c
Common mistake
print(", ".join([1, 2, 3]))
Python stops with:
TypeError: sequence item 0: expected str instance, int found
Every item must be a string. Store them as strings (["1", "2", "3"]), or build the text with an f-string.
See also
Last updated 2 weeks ago