Python Glossary Русский

all Python 3Strings (str)function

str.rsplit

splitting

Like split, but split from the right when maxsplit is given.

Syntax

s.rsplit(sep=None, maxsplit=-1)

Without maxsplit, rsplit and split produce identical lists — the difference shows up only when you cap the number of splits and specifically want the tail: the last field of a log line, a file extension, the domain of an address. Note the two separator modes: with no sep (or sep=None) runs of whitespace collapse and no empty pieces appear, while an explicit separator yields an empty string for every repeat of it.

Examples

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

See also

Open in the full glossarydocs.python.orgReport an inaccuracy