str.rpartition()
str.rpartition(sep) tuple[str, str, str] · Added in v3.x · Updated March 13, 2026 · String Methods The .rpartition() method splits a string into three parts using a separator, searching from the right side of the string. It returns a tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, the tuple contains two empty strings followed by the original string.
This method is useful when you need to split from the last occurrence of a separator rather than the first.
Syntax
str.rpartition(sep)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| sep | str | — | The separator string to split on |
Return Value
Returns a tuple of three strings: (before_sep, sep, after_sep). If the separator is not found, returns (”, ”, original_string).
Examples
Basic usage
text = "hello world"
result = text.rpartition(" ")
print(result)
# ('hello', ' ', 'world')
The separator appears as the middle element in the tuple.
Splitting from the right
path = "/usr/local/bin/python"
result = path.rpartition("/")
print(result)
# ('/usr/local', '/', 'bin/python')
The split happens at the last occurrence of the separator, not the first.
Working with multiple separators
filename = "document.final.pdf"
result = filename.rpartition(".")
print(result)
# ('document.final', '.', 'pdf')
This extracts the file extension reliably, regardless of dots in the filename.
Separator not found
text = "hello"
result = text.rpartition(" ")
print(result)
# ('', '', 'hello')
When the separator doesn’t exist, the original string becomes the last element.
Using with email addresses
email = "user@subdomain.example.com"
local, sep, domain = email.rpartition("@")
print(domain)
# subdomain.example.com
Getting the domain from the right side works even with multiple @ symbols.
Common Patterns
Extracting file components
def get_extension(filename):
name, sep, ext = filename.rpartition(".")
return ext if sep else ""
print(get_extension("image.png"))
# png
print(get_extension("archive.tar.gz"))
# gz
print(get_extension("nodots"))
#
This pattern reliably extracts the final extension.
Path manipulation
def get_directory(path):
head, sep, tail = path.rpartition("/")
return head if sep else "."
print(get_directory("/home/user/documents/file.txt"))
# /home/user/documents
print(get_directory("file.txt"))
# .
Useful for extracting the directory portion of a file path.
Splitting key-value pairs
def split_last(s, delimiter):
left, sep, right = s.rpartition(delimiter)
if sep:
return left, right
return None, s
print(split_last("a=b=c", "="))
# ('a=b', 'c')
print(split_last("novalue", "="))
# (None, 'novalue')
Splitting from the right is handy for parsing nested delimiters.
Difference from partition()
The two methods differ only in which occurrence they split on:
# partition splits from the left
"a-b-c".partition("-")
# ('a', '-', 'b-c')
# rpartition splits from the right
"a-b-c".rpartition("-")
# ('a-b', '-', 'c')
# both return 3-tuple when not found
"abc".partition("-")
# ('abc', '', '')
"abc".rpartition("-")
# ('', '', 'abc')
Use .partition() when you need the first split, and .rpartition() when you need the last split.