Technical skill practice

Python join(): Combine Text Fields with a Separator

Python str.join combines an iterable of strings into one string, inserting the string on the left between neighboring items. Write separator.join(parts), not parts.join(separator). It does not automatically convert integers to text, and it does not add a trailing separator.

Start with a small, explicit task

Create one display line from a fictional team name, task label and count. Keep the count numeric until the presentation step. Check a one-item result and empty input, then deliberately attempt an unconverted integer so the type requirement is visible.

Run the complete example

This original example was checked with Python 3.12.14. Save it as python_join.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.

Expected output

Team A | checked | 7

''

non-string rejected

parts = ["Team A", "checked", str(7)]
line = " | ".join(parts)
assert line == "Team A | checked | 7"
assert ",".join(["only"]) == "only"
assert ",".join([]) == ""
try:
    ",".join(["count", 7])
except TypeError:
    error = "non-string rejected"
else:
    raise AssertionError("expected TypeError")
print(line)
print(repr(",".join([])))
print(error)

Explain the result

There are three parts and two inserted separators. The count becomes text through str before join sees it; that conversion is a visible presentation choice rather than a side effect of joining. One item needs no separator, and no items produce an empty string. repr makes that empty result visible in the output.

The intentional failing call contains an integer and is caught as TypeError. This is different from a missing-value policy: converting None would yield the literal text None, which may not be the desired report. Decide how absent values should appear before converting them. Joining is suitable for a display line, but a CSV file with quoting requirements should use the csv module rather than a hand-built delimiter rule.

Avoid these mistakes

The separator is the object with the join method. Passing a single string as parts iterates its characters, which can insert separators inside a word. Also distinguish joining text from appending an item to a list: the former returns a new string, while the latter changes a list.

Questions, answered

Does join add a separator after the last item?

No. This three-part example inserts separators only between the parts.

Can I join integers directly?

Not with str.join. Convert intentionally or format each field first; otherwise the example demonstrates TypeError.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.