str.join()
str.join(iterable) Returns:
str · Updated March 13, 2026 · String Methods strings iteration joining
The .join() method concatenates elements from an iterable (such as a list, tuple, or generator) into a single string, inserting a separator between each element. It’s one of the most efficient ways to build strings from collections in Python.
Syntax
str.join(iterable)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
iterable | iterable | — | An iterable (list, tuple, set, generator) containing string elements to join |
Returns: str — A single string with all elements joined by the separator.
Examples
Basic usage
words = ["hello", "world", "python"]
# Join with a space separator
result = " ".join(words)
print(result)
# hello world python
# Join with a comma separator
result = ",".join(words)
print(result)
# hello,world,python
Joining tuples
fruits = ("apple", "banana", "cherry")
result = " - ".join(fruits)
print(result)
# apple - banana - cherry
Building file paths
path_parts = ["home", "user", "documents", "file.txt"]
# Using forward slash
result = "/".join(path_parts)
print(result)
# home/user/documents/file.txt
Practical CSV-like output
data = [
["name", "age", "city"],
["Alice", "30", "NYC"],
["Bob", "25", "LA"]
]
for row in data:
print(",".join(row))
# name,age,city
# Alice,30,NYC
# Bob,25,LA
Using with dictionary keys
config = {"host": "localhost", "port": "8080"}
# Join dictionary keys
result = ", ".join(config.keys())
print(result)
# host, port
# Join dictionary values
result = ", ".join(config.values())
print(result)
# localhost, 8080
String building in loops
# Building a long string efficiently
words = ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]
# Instead of: result = ""
# for word in words: result += word + " "
# Use join (much more efficient):
result = " ".join(words) + " "
print(result)
# the quick brown fox jumps over the lazy dog
Common Patterns
Filtering and joining
files = ["report.pdf", "image.png", "data.csv", "script.py"]
# Join only files with specific extensions
pdfs = ", ".join(f for f in files if f.endswith(".pdf"))
print(pdfs)
# report.pdf
Reverse operation with split
text = "hello world python"
# Split into list
words = text.split(" ")
print(words)
# ['hello', 'world', 'python']
# Rejoin with different separator
result = "-".join(words)
print(result)
# hello-world-python
Creating formatted output
headers = ["Name", "Age", "Occupation"]
values = ["John", "28", "Developer"]
# Create a formatted table row
row = " | ".join(values)
print(row)
# John | 28 | Developer
# Create header separator
separator = "-" * len(row)
print(separator)
# -------------------
Errors
All elements in the iterable must be strings. Passing non-string elements raises a TypeError:
numbers = [1, 2, 3]
",".join(numbers)
# TypeError: sequence item 0: expected str instance, int found
# Convert to strings first:
result = ",".join(str(n) for n in numbers)
print(result)
# 1,2,3
Joining an empty iterable returns an empty string:
result = ",".join([])
print(result)
# (empty string)
result = ",".join("")
print(result)
# (empty string)
Performance
.join() is significantly faster than string concatenation in loops because it pre-allocates memory for the final string. This makes it the preferred method for building strings from multiple elements.
# Slow - creates many intermediate string objects
result = ""
for word in words:
result += word + " "
# Fast - single allocation
result = " ".join(words)