Skip to content

Module 7 · Strings — topic 14 of 22

join()

separator.join(iterable) combines strings with a separator between them. Learn why the separator comes first, how to join non-strings, and join() as the efficient way to build text.

Beginner 10 min read·Lesson 95 of 306 in Python Programming

Learning objectives

  • Join a list of strings with any separator
  • Convert non-string items before joining
  • Build multi-line text efficiently

Definition

separator.join(iterable) combines strings with a separator between them. Learn why the separator comes first, how to join non-strings, and join() as the efficient way to build text.

Introduction

join() is the opposite of split(). It takes an iterable of strings and glues them together, putting the separator string between each pair: ", ".join(["a", "b", "c"]) gives "a, b, c".

Why it matters

Producing CSV lines, sentences from words, paths from parts and reports from lines are everyday tasks. join() handles separators correctly (no trailing comma) and is the fastest way to combine many pieces.

Detailed explanation

join() and split() form a pair that is worth remembering together: split() turns text into a list you can edit, and join() turns the edited list back into text. Many text transformations follow this split–change–join pattern, such as capitalising every word, removing unwanted words or reversing word order.

Because join() requires strings, it acts as a useful check: a TypeError points directly to a value you forgot to convert.

The method is called on the separator, not on the list: "-".join(parts). This reads as “join these parts with a hyphen”.

Every item must be a string. Joining numbers raises TypeError; convert them first, for example ", ".join(str(n) for n in numbers).

The separator can be empty (""), a single character, or any text such as " and " or "\n".

Any iterable works: a list, a tuple, a generator expression, or a string (which joins its characters).

Joining an empty list gives ""; joining one item gives that item with no separator.

join() builds the result in one step, which is much faster than repeated += when there are many pieces.

Syntax

Python
text = separator.join(strings)
csv_line = ",".join(fields)
lines = "\n".join(rows)
  • Call join on the separator.
  • Pass an iterable of strings.
  • No separator is added at the ends.

Code examples

Different separators

Python
words = ["learn", "build", "grow"]
print(" ".join(words))
print(", ".join(words))
print("".join(words))
print("-".join("abc"))
Output
learn build grow
learn, build, grow
learnbuildgrow
a-b-c

Joining numbers

Python
numbers = [4, 8, 15]
try:
", ".join(numbers)
except TypeError:
print(", ".join(str(n) for n in numbers))
Output
4, 8, 15

More examples

Building a report and a natural list

Python
rows = [("Asha", 82), ("Ben", 67), ("Chen", 91)]
report_lines = [f"{name:<6}{mark:>4}" for name, mark in rows]
report = "\n".join(["Name Mark", "-" * 10] + report_lines)
print(report)
def natural_list(items):
if len(items) <= 1:
return "".join(items)
return ", ".join(items[:-1]) + " and " + items[-1]
print(natural_list(["tea", "coffee", "juice"]))
print(natural_list(["tea"]))
Output
Name   Mark
----------
Asha    82
Ben     67
Chen    91
tea, coffee and juice
tea

Step-by-step explanation

  • Each row is formatted into a fixed-width line with an f-string.
  • The header, separator and data lines are combined in one list and joined with newlines.
  • natural_list joins all but the last item with commas and adds “and” before the last.
  • With one item, join returns just that item.

Expected output

Running “Building a report and a natural list” prints exactly:

TextOutput
Name Mark
----------
Asha 82
Ben 67
Chen 91
tea, coffee and juice
tea

Practical applications

  • CSV exporters join fields with commas and rows with newlines.
  • Email tools join recipient addresses with ", ".
  • Path-building code joins folder names, although os.path.join and pathlib are better for real paths.

Best practices

  • Use join for any list of strings.
  • Convert items with a generator expression.
  • Collect pieces in a list, then join once.

Common mistakes

Calling join on the list

Avoid

words.join(" ")

Prefer

" ".join(words)

Lists have no join method; the separator string does.

Adding the separator manually in a loop

Avoid

for w in words:
s += w + ", "

Prefer

s = ", ".join(words)

The loop leaves a trailing separator and is slower.

Key takeaways

  • separator.join(iterable) combines strings with a separator.
  • All items must be strings.
  • join is the efficient way to build long text.

Try it yourself

  1. 1
    Turn the list ["2026", "11", "22"] into "2026-11-22".
  2. 2
    Join the numbers 1 to 10 into one string separated by " + ".

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic