Python Glossary Русский

all Python 3Strings (str)function

str.rpartition

splitting

Split at the last occurrence of the separator into a (head, sep, tail) tuple.

Syntax

s.rpartition(sep)

The result is always a three-item tuple, so unpacking into three names never blows up on unexpected input — unlike rsplit, whose list length depends on whether the separator was found. If the separator is missing, the first two items come back empty and the whole string lands in the third (partition puts the string first instead). An empty separator raises ValueError rather than splitting per character.

Examples

print('a=b=c'.rpartition('='))  # → ('a=b', '=', 'c')
print('abc'.rpartition('='))    # → ('', '', 'abc')
print('a/b/c'.rpartition('/'))  # → ('a/b', '/', 'c')

See also

Open in the full glossarydocs.python.orgReport an inaccuracy