Python Glossary Русский

all Python 3Byte sequencesfunction

bytes.partition

bytes — split and join

Split at the first separator into (head, sep, tail).

Syntax

b.partition(...)

The result is always a 3-tuple, so unpacking into three names is safe; to tell whether the separator was found, test the middle element, not the length. Unlike split it keeps the separator and cuts exactly once, which makes it the right tool for key=value parsing where the value may itself contain the separator. An empty separator raises ValueError.

Examples

print(b'a=b=c'.partition(b'='))   # → (b'a', b'=', b'b=c')
print(b'name=Ann'.partition(b'=')[2])  # → b'Ann'
print(b'abc'.partition(b'='))  # → (b'abc', b'', b'')
print(b'=abc'.partition(b'='))  # → (b'', b'=', b'abc')
print(b'a=b=c'.rpartition(b'='))  # → (b'a=b', b'=', b'c')

See also

Open in the full glossarydocs.python.orgReport an inaccuracy