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.

PHP
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.

Python
"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'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.
  • PHP's explode() with a negative limit removes that many elements from the end of the result; Python's split() has no negative-limit behavior at all — you'd slice the resulting list manually, e.g. result[:-1].
  • PHP's explode("", $str) throws a ValueError-equivalent (PHP ValueError since PHP 8) for an empty separator; Python's str.split("") also raises ValueError: empty separator — both reject this case, though for a character-by-character split Python offers list(str) instead.

Examples

Basic split

PHP
explode(",", "a,b,c");
// ["a", "b", "c"]
Python
"a,b,c".split(",")
# ['a', 'b', 'c']
A direct, exact translation with no limit argument involved.

Limit argument (off-by-one counting)

PHP
explode(",", "a,b,c,d", 2);
// ["a", "b,c,d"]
Python
"a,b,c,d".split(",", 1)
# ['a', 'b,c,d']
PHP's limit of 2 means "2 pieces total"; Python's maxsplit of 1 means "1 split performed", which also yields 2 pieces — the numbers differ (2 vs. 1) even though the result is identical.

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.