Python Glossary Русский

all Python 3Byte sequencesfunction

bytearray.partition

bytearray — split and join

Split at the first separator into a triple.

Syntax

ba.partition(...)

You always get exactly three parts, so unpacking head, sep, tail can never fail — check the middle item to learn whether the separator was actually found, rather than inspecting a length. The search takes the leftmost occurrence; use rpartition for the rightmost. An empty separator is not "split everywhere" — it raises ValueError.

Examples

print(bytearray(b'a=b').partition(b'='))   # → (bytearray(b'a'), bytearray(b'='), bytearray(b'b'))
print(bytearray(b'key=1=2').partition(b'='))   # → (bytearray(b'key'), bytearray(b'='), bytearray(b'1=2'))
print(bytearray(b'name=Alice').partition(b'=')[2])   # → bytearray(b'Alice')
print(bytearray(b'noeq').partition(b'='))   # → (bytearray(b'noeq'), bytearray(b''), bytearray(b''))
print(bytearray(b'a=b=c').rpartition(b'='))   # → (bytearray(b'a=b'), bytearray(b'='), bytearray(b'c'))
print(bytearray(b'ab').partition(b''))   # → ValueError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy