Python Glossary Русский

all Python 3Byte sequencesfunction

bytes.rsplit

bytes — split and join

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

Syntax

b.rsplit(...)

Without maxsplit the result is identical to split — the direction only matters once you cap the number of splits. That is exactly when to reach for it: when you care about the tail, such as a file extension or the last field of a line. With None as separator runs of whitespace collapse and leading/trailing whitespace is ignored; with an explicit separator every occurrence cuts on its own.

Examples

print(b'a b c'.rsplit(None, 1))   # → [b'a b', b'c']
print(b'a,b,c'.rsplit(b','))   # → [b'a', b'b', b'c']
print(b'a,b,c'.rsplit(b',', 1))   # → [b'a,b', b'c']
print(b'archive.tar.gz'.rsplit(b'.', 1))   # → [b'archive.tar', b'gz']
print(b''.rsplit(b','))   # → [b'']
print(b'a,b'.rsplit(','))   # → TypeError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy