Python bytes.split() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
bytes method

Definition

bytes.split() returns a list of bytes objects. With a separator, such as b",", it cuts at every occurrence and keeps empty fields between adjacent separators.

With no separator (or None) it splits on runs of ASCII whitespace and drops empty pieces, which is perfect for space-separated protocol lines. maxsplit limits how many cuts are made, counting from the left.

bytes.split() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.split(...). No import is needed, and bytearray objects have the same method.

Syntax

Split on whitespace, or on an exact separator, optionally limiting the number of splits.

python
parts = data.split()
parts = data.split(sep)
parts = data.split(sep, maxsplit)
CallWhat it does
b"a b c".split()Returns [b'a', b'b', b'c'].
b"a,,b".split(b",")Returns [b'a', b'', b'b'].
b"k=v=w".split(b"=", 1)Returns [b'k', b'v=w'].

The separator can be several bytes long, such as b"\r\n"; it is matched as a whole sequence.

Method signatures
python
bytes.split(sep=None, maxsplit=-1)
# sep: bytes-like separator, or None for runs of ASCII whitespace
# returns a list of bytes objects

Parameters

ParameterDescription
sepOptional. The separator bytes. None (default) splits on runs of whitespace.
maxsplitOptional. Maximum number of splits; -1 (default) means no limit.

Return Value

Return ValueDescription
listA list of bytes objects (bytearray objects when called on a bytearray).

Python Examples

Example 1 — Unpack an HTTP Request Line

A request line has three space-separated fields. Split it on whitespace, then compare with splitting on a slash and limiting the number of splits.

python
line = b"GET /docs/intro HTTP/1.1"

method, target, version = line.split()
print(method, target, version)
print(line.split(b"/"))
print(line.split(b" ", 1))

How It Works

  1. Unpack directly. Exactly three fields come back, so they unpack into three names.
  2. Any separator. Splitting on b"/" cuts in three places, and the spaces stay inside the pieces.
  3. Limit the cuts. maxsplit=1 separates the method and keeps the rest of the line together.

Example 2 — Whitespace Splitting vs an Explicit Separator

See how the default mode ignores repeated whitespace, while an explicit separator keeps every empty field.

python
data = b"  a  b\tc \n"

print(data.split())
print(data.split(b" "))
print(b"1,,2,".split(b","))

How It Works

  1. Runs collapse. With no argument, leading, trailing, and repeated whitespace (spaces, tab, newline) never produce empty pieces.
  2. Only spaces count. b" " splits on spaces only, so the tab and newline stay inside pieces and empty fields appear.
  3. CSV-style fields. Adjacent and trailing commas produce empty bytes, which keeps column positions correct.

Example 3 — Parse Query-Style Key-Value Pairs

Split a query on &, then split each pair into key and value with partition() so a missing value does not crash.

python
query = b"user=ada&lang=py&debug"

params = {}
for pair in query.split(b"&"):
    key, _, value = pair.partition(b"=")
    params[key] = value
print(params)
print(b"".split(b","), b"".split())

How It Works

  1. Pairs first. Splitting on & gives three pair strings as bytes.
  2. Safe unpacking. partition() always returns three items, so debug gets an empty value instead of raising.
  3. bytes as dict keys. bytes are hashable, so they work as dictionary keys.
  4. Empty input. With a separator you get one empty field; in whitespace mode you get an empty list.

Frequently Asked Questions

A list of bytes objects. Called on a bytearray, it returns a list of bytearray objects.
split() splits on runs of any ASCII whitespace and drops empty pieces; split(b" ") splits on every single space and keeps empty pieces.
Pass maxsplit: data.split(b"=", 1). partition() is another good choice for a single split.
The separator is a str. Use b",".
Use splitlines(), which handles \n, \r\n, and \r and does not add an empty item for a final newline.

Next: bytes.splitlines()

Split bytes into lines at \n, \r\n, and \r boundaries.

splitlines() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful