Python Glossary Русский

all Python 3Byte sequencesfunction

bytes.split

bytes — split and join

Split the bytes on a separator (whitespace by default).

Syntax

b.split(...)

The separator must be bytes-like: split(',') with a str, or split(44) with a byte value, will not work. Calling it with no argument differs from passing b' ': the no-argument form collapses runs of whitespace and drops leading and trailing ones, while an explicit separator cuts at every occurrence and can yield empty pieces. On empty input b''.split(b',') returns a list with one empty item, whereas b''.split() returns an empty list.

Examples

print(b'a b c'.split())   # → [b'a', b'b', b'c']
print(b'a,b,c'.split(b','))   # → [b'a', b'b', b'c']
print(b'a,b,c'.split(b',', 1))   # → [b'a', b'b,c']
print(b'key=value'.split(b'=', 1))   # → [b'key', b'value']
print(b'a,,b'.split(b','))   # → [b'a', b'', b'b']
print(b'  a  b  '.split(b' '))   # → [b'', b'', b'a', b'', b'b', b'', b'']

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy