PHP implode() Equivalent in Python

Python's str.join() does the same job as PHP's implode(), but the separator and the list swap positions, and Python requires every element to already be a string.

Close equivalent Covers the common case, but has documented behavioral differences.

PHP source
implode()
Closest Python API
str.join()

Main limitation: PHP `implode($glue, $array)` takes the separator first, then the array; Python `sep.join(iterable)` is called *on* the separator string, with the iterable as the argument — the calling convention is inverted, not just the argument order.

How to use each function

Before comparing the two, here's how implode() and str.join() work on their own.

PHP: implode()

implode(string $separator, array $array): string

Joins the elements of an array into a single string, placing $separator between each element. Non-string elements (integers, floats, booleans) are automatically converted to strings. The $separator may be omitted, defaulting to an empty string.

Returns: The concatenated string.

PHP
implode(", ", ["red", "green", "blue"]);
// "red, green, blue"

Python: str.join()

separator.join(iterable)

Joins the strings in iterable using separator, called as a method on the separator itself rather than passed as an argument to a free function. Every element must already be a str — non-string items raise a TypeError and must be converted first, e.g. with str() inside a generator expression.

Returns: The concatenated string.

Python
", ".join(["red", "green", "blue"])
# 'red, green, blue'

Quick mapping

PHP implode()
Closest Python API str.join()

How it differs

  • PHP implode($glue, $array) takes the separator first, then the array; Python sep.join(iterable) is called *on* the separator string, with the iterable as the argument — the calling convention is inverted, not just the argument order.
  • PHP automatically converts non-string elements (integers, floats, booleans) to strings when joining; Python's str.join() raises a TypeError if any element isn't already a str — you must map elements through str() first, e.g. ",".join(str(x) for x in items).
  • PHP's implode($array) allows omitting the glue entirely, defaulting to an empty string separator; Python has no such shorthand — you must always pass an explicit separator, even if it's "".

Examples

Basic join

PHP
implode(", ", ["a", "b", "c"]);
// "a, b, c"
Python
", ".join(["a", "b", "c"])
# "a, b, c"
Same result, but note the separator is the receiver of the method call in Python, not an argument.

Joining numbers (requires explicit conversion in Python)

PHP
implode(",", [1, 2, 3]);
// "1,2,3"
Python
",".join(str(n) for n in [1, 2, 3])
# "1,2,3"
PHP silently stringifies the integers; Python's join() would raise a TypeError without the explicit str() conversion inside the generator expression.

Edge cases

Edge case

Joining an empty array/list produces an empty string in both languages — this edge case matches exactly.

Edge case

A common Python mistake when porting: forgetting to convert numbers to strings before joining raises TypeError: sequence item 0: expected str instance, int found — always wrap with map(str, ...) or a generator expression when the source list might contain non-strings.

References

  • PHP Manual — implode — checked 2026-07-29
  • Python docs — str.join() (v3.x) — checked 2026-07-29

Last verified 2026-07-29.