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.
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.
", ".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; Pythonsep.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 aTypeErrorif any element isn't already astr— you must map elements throughstr()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
implode(", ", ["a", "b", "c"]);
// "a, b, c" ", ".join(["a", "b", "c"])
# "a, b, c" Joining numbers (requires explicit conversion in Python)
implode(",", [1, 2, 3]);
// "1,2,3" ",".join(str(n) for n in [1, 2, 3])
# "1,2,3" 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.