bytes.split()

The workhorse for chopping up binary records. Note that splitting on a missing separator is not an error — you get a one-item list back.

Bytes methodPython 3.0+Live demo
Common call
data.split(b'\n')
Returns
list of bytes; never empty
Replaces
a manual find-and-slice loop
Watch out
an empty separator raises; data.split('') is doubly wrong
bytes.split(sepsep — Separator to split on. Omitted or None splits on runs of ASCII whitespace and drops empty parts.type: bytes | None · default: None=None, maxsplitmaxsplit — Maximum number of splits. The remainder stays in the final element. Negative means no limit.type: int · default: -1=-1)
→ list

Demo

Live evaluation
Try:
Inputs
sstrdata (encoded as utf-8)
sepstrseparator
Output
bytes('a,b,c', 'utf-8').split(bytes(',', 'utf-8'))
[b'a', b'b', b'c']

Splitting on an explicit separator keeps every field, including empty ones — "a,,b" gives three parts with an empty middle. A separator that is not present is not an error: you get a single-item list holding the whole input, which is what makes unpacking into a fixed number of names fail unpredictably. An empty separator is a ValueError, because it would produce an infinite number of splits.

Parameters

NameTypeRequiredDescription
sepbytes | Noneno (None)Separator to split on. Omitted or None splits on runs of ASCII whitespace and drops empty parts.
maxsplitintno (-1)Maximum number of splits. The remainder stays in the final element. Negative means no limit.

Return value

list — A list of bytes objects. Always contains at least one item — the whole input when the separator is absent.

Common patterns

Split lines from a binary read
Files opened in binary mode give bytes, so the separator must be bytes too.
lines = data.split(b'\n')
Split off a header once
maxsplit stops after the first separator, keeping the rest intact.
head, body = data.split(b'\r\n\r\n', 1)
Tokenise on whitespace
The no-argument form collapses runs and drops empties.
fields = data.split()

Examples

1. Comma separated
b'a,b,c'.split(b',')
Returns
[b'a', b'b', b'c']
2. Absent separator
b'abc'.split(b',')
Returns
[b'abc']
3. Empty input
b''.split(b',')
Returns
[b'']
4. Adjacent keeps empties
b'a,,b'.split(b',')
Returns
[b'a', b'', b'b']
5. Whitespace form
b' a b '.split()
Returns
[b'a', b'b']
6. Empty separator
b'abc'.split(b'')
Returns
ValueError: empty separator

Pitfalls

1. A missing separator gives one item, not an error
Unpacking into a fixed number of names then fails with a confusing message about values to unpack, pointing at the assignment rather than at the malformed input.
Unpack fails
k, v = b'novalue'.split(b'=')
ValueError: not enough values to unpack (expected 2, got 1)
Use partition
k, sep, v = b'novalue'.partition(b'=')
always three parts
2. Explicit separators keep empty fields
Only the no-argument whitespace form collapses runs. Splitting "a,,b" on a comma deliberately keeps the empty middle field, which is right for CSV and surprising everywhere else.
Empty field kept
b'a,,b'.split(b',')
[b'a', b'', b'b']
Filter them out
[p for p in b'a,,b'.split(b',') if p]
[b'a', b'b']
3. The separator must be bytes
Passing a str is a TypeError. This bites constantly when a file is opened in binary mode but the parsing code was written for text.
str rejected
b'a,b'.split(',')
TypeError: a bytes-like object is required, not 'str'
Use a bytes literal
b'a,b'.split(b',')
[b'a', b'b']
4. An empty separator raises
There is no sensible answer, so Python refuses rather than inventing one. A separator built from a variable that turned out empty fails here rather than where it was set.
No answer possible
b'abc'.split(b'')
ValueError: empty separator
Guard it
parts = data.split(sep) if sep else [data]
explicit

When to use

Use it
  • Chopping a binary payload into records or lines
  • Parsing simple delimited formats read in binary mode
  • Splitting off a header with maxsplit
Reach for something else
  • The data is text → decode first and use str.split
  • You want exactly two parts → partition, which never fails to unpack
  • Line splitting that must handle CRLF → splitlines

Notes

Complexity
O(n) — one scan, plus an allocation per part
Return
A new list of new bytes objects; the original is unchanged
CPython impl
Objects/bytesobject.c :: bytes_split
Memory
Allocates the list and every part; splitting a huge buffer doubles memory
Thread-safe
Yes — bytes are immutable

FAQ

Because there is one field, and it happens to be empty. b''.split(b',') is [b''], not []. The rule is that an explicit separator always produces one more part than there are separators.

b''.split(b',')
# [b'']

History

3.0
bytes.split arrived with the bytes type in the text/binary split.