all Python 3Byte sequencesfunction
bytearray.rpartition
bytearray — split and join
Split at the last separator into a triple.
Syntax
ba.rpartition(...)
The result is always a 3-tuple, so head, sep, tail = ba.rpartition(b'=') can never fail to unpack. When the separator is absent, though, it is the first two parts that come back empty and the original content that lands in the third — the mirror image of partition. Test the middle element to decide whether a match happened; never rely on tuple length or on head being non-empty.
Examples
print(bytearray(b'a=b=c').rpartition(b'=')) # → (bytearray(b'a=b'), bytearray(b'='), bytearray(b'c'))
print(bytearray(b'a=b=c').partition(b'=')) # → (bytearray(b'a'), bytearray(b'='), bytearray(b'b=c'))
print(bytearray(b'key: value').rpartition(b': ')[2]) # → bytearray(b'value')
print(bytearray(b'/usr/local/bin').rpartition(b'/')[2]) # → bytearray(b'bin')
print(bytearray(b'abc').rpartition(b'=')) # → (bytearray(b''), bytearray(b''), bytearray(b'abc'))
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy