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

ParameterDescription
sep The separator to look for.

Return value

A tuple (before, sep, after); if sep isn't found: ('', '', string).

Examples

Split at the last dotTry it yourself
print("archive.tar.gz".rpartition("."))

Output

('archive.tar', '.', 'gz')
Name and extensionTry it yourself
filename = "photo.final.jpg"
name, dot, ext = filename.rpartition(".")
print(name, ext)

Output

photo.final jpg

Version history

VersionChange
Python 3.0Added