str.rsplit()

Split from the right — only meaningfully different from split when maxsplit is used.

String methodPython 2.4+Live demo
Common call
user, host = email.rsplit("@", 1)
Returns
a list of strings, length ≤ maxsplit + 1
Replaces
the rfind + slice pattern for "keep the head intact, split the tail"
Watch out
sep=None collapses runs of whitespace AND strips ends; sep="" raises ValueError
str.rsplit(sepsepDelimiter. None (default) splits on any whitespace runs and strips leading/trailing whitespace. Empty string raises ValueError.type: str | None · default: None=None, maxsplitmaxsplitMaximum number of splits from the RIGHT. -1 (default) means no limit. When set, the head keeps the un-split remainder as one piece.type: int · default: -1=-1)
list[str]

Demo

Live evaluation
Try:
Inputs
stringstrthe source
sepstrempty = whitespace
maxsplitintempty = unlimited
Output
'a,b,c,d'.rsplit(',')
['a', 'b', 'c', 'd']

rsplit only differs from split when maxsplit is set. With maxsplit=N, the LAST N delimiters produce splits — the head accumulates everything before those into a single piece. sep=None (the empty demo input) collapses whitespace runs AND trims ends, exactly like split. Empty sep string (only when specified explicitly) raises ValueError.

Parameters

NameTypeRequiredDescription
sepstr | Noneno (None)Delimiter. None (default) splits on any whitespace runs and strips leading/trailing whitespace. Empty string raises ValueError.
maxsplitintno (-1)Maximum number of splits from the RIGHT. -1 (default) means no limit. When set, the head keeps the un-split remainder as one piece.

Return value

list[str]A list of the parts. With no maxsplit (or -1), identical to split. With maxsplit set, splits at most maxsplit times FROM THE RIGHT — the last N pieces get their own slots, the head keeps everything else.

Common patterns

Email → user + host
The last "@" separates the host, even if the local part contains "@" (rare but legal).
user, host = addr.rsplit("@", 1)
Directory + basename
Slice off the final path component, keep the rest intact.
directory, name = path.rsplit("/", 1)
Last-N fields
Grab the tail — say the last two CSV fields — without touching the head.
head, last_but_one, last = line.rsplit(",", 2)

Examples

1. No maxsplit = same as split
"a,b,c,d".rsplit(",")
Returns
["a", "b", "c", "d"]
2. maxsplit=1 keeps head
"a,b,c,d".rsplit(",", 1)
Returns
["a,b,c", "d"]
3. maxsplit=2
"a,b,c,d".rsplit(",", 2)
Returns
["a,b", "c", "d"]
4. Whitespace sep
"first middle last".rsplit(None, 1)
Returns
["first middle", "last"]
5. Email split
"user@corp@example.com".rsplit("@", 1)
Returns
["user@corp", "example.com"]
6. No match
"nohits".rsplit(",")
Returns
["nohits"]

Pitfalls

1. Without maxsplit, rsplit is a no-op vs split
Reaching for rsplit "because I want to split from the right" without passing maxsplit gives you the same list as split. The value is in the maxsplit case.
No difference
"a,b,c".rsplit(",")
"a,b,c".split(",")
["a", "b", "c"] # identical
With maxsplit
"a,b,c".rsplit(",", 1)
["a,b", "c"] # tail split off
2. Empty sep raises ValueError
Same rule as split — cannot split by an empty separator. sep=None (default) is fine and means "any whitespace runs".
Runtime error
"hello".rsplit("")
ValueError: empty separator
Use None for whitespace
"hello world".rsplit(None, 1)
["hello", "world"]
3. sep=None strips leading/trailing whitespace AND collapses runs
sep=None is not the same as sep=" ". It collapses runs of any whitespace and trims the ends. sep=" " keeps empty strings from consecutive spaces and does not strip.
sep=" "
"  a   b  ".rsplit(" ")
["", "", "a", "", "", "b", "", ""]
sep=None
"  a   b  ".rsplit(None)
["a", "b"]
4. maxsplit counts SPLITS, not pieces
maxsplit=N means at most N splits — producing at most N+1 pieces. Off-by-one bugs are common.
Off by one
"a,b,c,d".rsplit(",", 3)  # expected 3 pieces?
["a", "b", "c", "d"] # 4 pieces
For N pieces, split N-1 times
"a,b,c,d".rsplit(",", 2)
["a,b", "c", "d"] # 3 pieces

When to use

Use it
  • Splitting off the last N fields while keeping the head intact
  • Splitting emails, URLs, or paths where the trailing part matters
  • Any "head + tail" parsing where the delimiter appears earlier in the head too
Reach for something else
  • No maxsplit → just use split, they are identical
  • Two-piece split at the first occurrence → split(sep, 1) instead
  • Fixed-shape three-piece result → rpartition
  • Splitting on regex → re.split

Notes

Complexity
O(n)
Return
A new list of strings; length is min(number-of-matches, maxsplit) + 1
CPython impl
Objects/unicodeobject.c :: unicode_rsplit
Memory
One list plus one substring per piece
Thread-safe
Yes — strings are immutable

FAQ

When maxsplit is set and you want the LAST N pieces isolated, with the head kept as a single string. Classic cases: an email where the local part could contain "@", a path where the base name is at the end, a compound identifier where you only want the suffix.

History

2.4
rsplit() introduced.