Python Glossary Русский

all Python 3Byte sequencesfunction

bytearray.rsplit

bytearray — split and join

Like split, but from the right with a max count.

Syntax

ba.rsplit(...)

With no maxsplit, rsplit gives exactly what split gives — it earns its keep only when you need to peel something off the end: the last path segment, a file extension, the trailing field of a record. The separator matters too: with None runs of whitespace collapse and no empty edge pieces appear, while an explicit b' ' turns two adjacent spaces into an empty bytearray between them.

Examples

print(bytearray(b'a b c').rsplit(None, 1))   # → [bytearray(b'a b'), bytearray(b'c')]
print(bytearray(b'a b c').rsplit())   # → [bytearray(b'a'), bytearray(b'b'), bytearray(b'c')]
print(bytearray(b'a,b,c').rsplit(b',', 1))   # → [bytearray(b'a,b'), bytearray(b'c')]
print(bytearray(b'a,b,c').split(b',', 1))   # → [bytearray(b'a'), bytearray(b'b,c')]
print(bytearray(b'name.tar.gz').rsplit(b'.', 1)[-1])   # → bytearray(b'gz')
print(bytearray(b'').rsplit(b','))   # → [bytearray(b'')]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy