Python 3 reference · String Methods
Python String rpartition() Method
Split the string at the last occurrence of a separator into a 3-tuple: before, separator and after.
Python 3 Since 3.0
Syntax
str.rpartition(sep)
Parameters
| Parameter | Description |
|---|---|
sep |
The separator to look for. |
Return value
A tuple (before, sep, after); if sep isn't found: ('', '', string).
Examples
print("archive.tar.gz".rpartition("."))
Output
('archive.tar', '.', 'gz')filename = "photo.final.jpg"
name, dot, ext = filename.rpartition(".")
print(name, ext)
Output
photo.final jpg
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
str.partition()· Split the string at the first occurrence of a separator into a 3-tuple: the part before, the separator, and the part after.str.rsplit()· Split the string into a list from the right. With maxsplit, only the last few splits are made.str.rfind()· Return the highest index where a substring is found, searching from the right, or -1 if it isn't found.- Python Strings · tutorial