Python bytes.partition() Method

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

Definition

bytes.partition(sep) searches for the first occurrence of sep and returns a 3-tuple: the bytes before it, the separator itself, and the bytes after it.

If the separator is not found, the result is (data, b'', b''). Because the tuple always has three items, you can unpack it safely without checking first, which makes it ideal for name: value header lines.

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

Syntax

Pass the separator and unpack the three parts.

python
before, sep, after = data.partition(separator)
CallWhat it does
b"k=v".partition(b"=")Returns (b'k', b'=', b'v').
b"k=v=w".partition(b"=")Returns (b'k', b'=', b'v=w').
b"kv".partition(b"=")Returns (b'kv', b'', b'').

Check whether the separator was found by testing the middle item: it is empty only when nothing matched.

Method signatures
python
bytes.partition(sep, /)
# returns a 3-tuple of bytes objects

Parameters

ParameterDescription
sepThe separator to search for (bytes-like, not empty).

Return Value

Return ValueDescription
tuple(before, sep, after) when found, or (data, b'', b'') when not found.

Python Examples

Example 1 — Parse an HTTP Header Line

Separate a header name from its value, then pull a parameter out of the value with a second partition.

python
header = b"Content-Type: text/html; charset=utf-8"

name, sep, value = header.partition(b": ")
print(name)
print(value)
print(value.partition(b"; ")[2])

How It Works

  1. Two-byte separator. b": " matches the colon and space together, so the value has no leading space.
  2. Only the first. Later separators stay inside value.
  3. Index the tuple. Item 2 of the second partition is the text after ;.

Example 2 — What partition() Returns When Nothing Matches

Partition three lines: a normal one, one without the separator, and one with two separators.

python
for line in [b"key=value", b"novalue", b"a=b=c"]:
    print(line.partition(b"="))

How It Works

  1. Normal split. Key, separator, and value fill the three slots.
  2. No separator. The whole line lands in the first slot and the other two are empty bytes.
  3. First match wins. The second = stays inside the value.

Example 3 — Split an HTTP Response into Head and Body

The blank line \r\n\r\n separates headers from the body. One partition splits them, and the middle item tells you it was found.

python
response = b"HTTP/1.1 200 OK\r\nServer: demo\r\n\r\n<h1>Hi</h1>"

head, sep, body = response.partition(b"\r\n\r\n")
print(head.split(b"\r\n"))
print(body)
print(bool(sep))

How It Works

  1. Four-byte separator. The first \r\n\r\n marks the end of the headers.
  2. Header lines. split() on b"\r\n" gives the status line and one header.
  3. Body untouched. Everything after the blank line is the body, even if it contains more CRLF pairs.
  4. Found flag. A non-empty separator is truthy, confirming the blank line was present.

Frequently Asked Questions

A 3-tuple of bytes: the part before the first separator, the separator, and the part after it.
You get (data, b"", b""). Check the middle item to detect it.
partition() always returns three items, including the separator; split(sep, 1) returns a list of one or two items.
Use rpartition().
Yes. Any non-empty bytes-like separator works, such as b"\r\n\r\n".

Next: bytes.removeprefix()

Remove an exact prefix once if present (Python 3.9+).

removeprefix() →

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.

12 people found this page helpful