str.rpartition()

Split the string at the LAST occurrence of sep, returning a fixed-shape three-tuple. The mirror of str.partition.

String methodPython 2.5+Live demo
Common call
name, _, ext = "archive.tar.gz".rpartition(".")
Returns
3-tuple, always — sep preserved in the middle
Replaces
the rfind + slice + guard pattern for "split at last delimiter"
Watch out
not-found puts the original in slot 2 (not slot 0 like partition!); empty sep raises ValueError
str.rpartition(sepsepSubstring to split on. Empty string raises ValueError. Last occurrence only.type: str · required)
tuple[str, str, str]

Demo

Live evaluation
Try:
Inputs
stringstrthe source
sepstrthe separator
Output
'archive.tar.gz'.rpartition('.')
['archive.tar', '.', 'gz']

rpartition splits at the LAST occurrence of sep — the mirror of partition. Always returns exactly three strings. When the separator is not found, the "original" slot moves: partition puts it at index 0, rpartition puts it at index 2. That asymmetry matches the direction each method scans from.

Parameters

NameTypeRequiredDescription
sepstryesSubstring to split on. Empty string raises ValueError. Last occurrence only.

Return value

tuple[str, str, str]Always a 3-tuple (before, sep, after). If sep is not found: ("", "", original_string) — note the original is in slot 2, unlike partition where it lands in slot 0.

Common patterns

File name + extension
The last dot separates the base name from the extension.
stem, _, ext = filename.rpartition(".")
Directory + basename
Split a path at its last slash.
dir_, _, base = path.rpartition("/")
Optional suffix strip
Strip only if present, leave the string alone otherwise — no branch.
head, sep, tail = s.rpartition(":")
tag = tail if sep else "default"

Examples

1. Last dot
"archive.tar.gz".rpartition(".")
Returns
("archive.tar", ".", "gz")
2. Key=value (only one)
"user=alice".rpartition("=")
Returns
("user", "=", "alice")
3. Last of many
"a=b=c".rpartition("=")
Returns
("a=b", "=", "c")
4. Not found → slot 2
"hello".rpartition(".")
Returns
("", "", "hello")
5. Separator at end
"trail=".rpartition("=")
Returns
("trail", "=", "")
6. Multi-char separator
"a->b".rpartition("->")
Returns
("a", "->", "b")
7. Empty sep raises
"hello".rpartition("")
Returns
ValueError: empty separator

Pitfalls

1. Not-found puts the original in slot 2 — the OPPOSITE of partition
partition and rpartition both return 3-tuples with the original when nothing matches, but they put it in different slots. Wired backwards, an rpartition user reading the "before" slot will always get an empty string on misses.
Wrong slot
before, _, _ = "hello".rpartition("=")
# before is ""
before = ""
Check sep
before, sep, after = s.rpartition("=")
value = after if sep else before
explicit fallback
2. Splits only at the LAST occurrence
Everything before the last sep stays in the head slot — useful when the value comes at the end, wrong when you meant to split every occurrence.
Not a full split
"a=b=c".rpartition("=")
("a=b", "=", "c")
Use split for all
"a=b=c".split("=")
["a", "b", "c"]
3. Empty separator raises ValueError
Same rule as partition — no sensible three-way split of an empty separator.
Runtime error
"hello".rpartition("")
ValueError: empty separator
Guard
if sep:
    a, s, b = text.rpartition(sep)
no-op when sep is empty
4. Splitting a filename with rpartition eats the dot
rpartition returns (stem, sep, ext) — the extension is WITHOUT the leading dot. If you need ".gz" not "gz", add it back.
No leading dot
"a.tar.gz".rpartition(".")[2]
"gz" # no leading "."
Rebuild
stem, sep, ext = s.rpartition(".")
full_ext = sep + ext  # ".gz"
".gz"

When to use

Use it
  • Splitting a filename at its final dot
  • Extracting basename from a path (rpartition("/") or ("\\"))
  • "Take the tail" parsing where the delimiter may appear multiple times
  • Getting a fixed-shape return you can always unpack into three names
Reach for something else
  • Full N-way split → split
  • First-occurrence split → partition
  • Regex-based splits → re.split
  • Filename splits where you care about ".tar.gz" as a compound → use pathlib's suffixes

Notes

Complexity
O(n) — one linear scan for the last match
Return
A 3-tuple of strings
CPython impl
Objects/unicodeobject.c :: unicode_rpartition
Memory
Two new substrings when the separator is found; no allocation otherwise
Thread-safe
Yes — strings are immutable

FAQ

They scan from opposite directions. partition splits at the FIRST occurrence and puts the original in slot 0 when missing. rpartition splits at the LAST occurrence and puts the original in slot 2 when missing. Pick based on where the delimiter of interest sits.

History

2.5
partition() and rpartition() introduced together.