PHP explode() Equivalent in Python
Python's str.split() covers the same job as PHP's explode(), but the two differ on how a positive vs. negative limit argument behaves.
Close equivalent Covers the common case, but has documented behavioral differences.
- PHP source
explode()- Closest Python API
str.split()
Main limitation: PHP's `explode($sep, $str, $limit)` with a positive limit caps the number of pieces returned, lumping the remainder into the last element; Python's `str.split(sep, maxsplit)` caps the number of *splits performed* (so `maxsplit=2` yields up to 3 pieces) — the counting convention is offset by one.
How to use each function
Before comparing the two, here's how explode() and str.split() work
on their own.
PHP: explode()
explode(string $separator, string $string, int $limit = PHP_INT_MAX): array
Splits a string into an array on a literal substring separator. A positive $limit caps the number of pieces returned, folding any remainder into the last element; a negative $limit removes that many elements from the end of the result.
Returns: An array of one or more string pieces.
explode(",", "red,green,blue");
// ["red", "green", "blue"] Python: str.split()
str.split(sep=None, maxsplit=-1)
Splits a string on a literal separator into a list of substrings. maxsplit caps the number of splits performed, not the number of pieces, so maxsplit=1 produces at most 2 elements. With no arguments it splits on any run of whitespace instead of a literal separator.
Returns: A list of one or more string pieces.
"red,green,blue".split(",")
# ['red', 'green', 'blue'] Quick mapping
| PHP | explode() |
|---|---|
| Closest Python API | str.split() |
How it differs
- PHP's
explode($sep, $str, $limit)with a positive limit caps the number of pieces returned, lumping the remainder into the last element; Python'sstr.split(sep, maxsplit)caps the number of *splits performed* (somaxsplit=2yields up to 3 pieces) — the counting convention is offset by one. - PHP's
explode()with a negative limit removes that many elements from the end of the result; Python'ssplit()has no negative-limit behavior at all — you'd slice the resulting list manually, e.g.result[:-1]. - PHP's
explode("", $str)throws aValueError-equivalent (PHPValueErrorsince PHP 8) for an empty separator; Python'sstr.split("")also raisesValueError: empty separator— both reject this case, though for a character-by-character split Python offerslist(str)instead.
Examples
Basic split
explode(",", "a,b,c");
// ["a", "b", "c"] "a,b,c".split(",")
# ['a', 'b', 'c'] Limit argument (off-by-one counting)
explode(",", "a,b,c,d", 2);
// ["a", "b,c,d"] "a,b,c,d".split(",", 1)
# ['a', 'b,c,d'] Edge cases
Edge case
Splitting an empty string (explode(",", "")) returns [""] in PHP — a one-element array containing an empty string; Python's "".split(",") also returns [''], so this edge case matches exactly.
Edge case
PHP's explode() only splits on a literal substring, never a pattern — same as Python's str.split(); if you need regex-based splitting, compare to PHP's preg_split() and Python's re.split() instead, which is a different function pairing entirely.
References
- PHP Manual — explode — checked 2026-07-29
- Python docs — str.split() (v3.x) — checked 2026-07-29
Last verified 2026-07-29.