Python bytes.rpartition() Method

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

Definition

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

If the separator is not found, the result is (b'', b'', data): the data goes in the last slot, the mirror image of partition(). It is a natural fit for trailing items such as a port number or a checksum.

bytes.rpartition() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.rpartition(...). 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.rpartition(separator)
CallWhat it does
b"a.b.c".rpartition(b".")Returns (b'a.b', b'.', b'c').
b"abc".rpartition(b".")Returns (b'', b'', b'abc').
b"x::y".rpartition(b":")Returns (b'x:', b':', b'y').

When the separator is missing, the data is in the third slot, not the first; handle that case explicitly.

Method signatures
python
bytes.rpartition(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) split at the last match, or (b'', b'', data) when not found.

Python Examples

Example 1 — Split Host and Port at the Last Colon

IPv6 addresses contain colons, so splitting at the last one is the right choice. A missing port shows the not-found layout.

python
for addr in [b"example.com:8080", b"[::1]:443", b"localhost"]:
    host, _, port = addr.rpartition(b":")
    print(host, port)

How It Works

  1. Simple case. The only colon divides host and port.
  2. IPv6 safe. The bracketed address has three colons, but only the last one separates the port.
  3. Watch the not-found case. Without a colon the name ends up in port and host is empty, so check the separator before using the parts.

Example 2 — partition() and rpartition() Side by Side

Split a file name with two dots both ways, then search for a separator that is not there.

python
name = b"photo.2026.raw"

print(name.partition(b"."))
print(name.rpartition(b"."))
print(name.rpartition(b"#"))

How It Works

  1. First dot. partition() keeps everything after the first dot together.
  2. Last dot. rpartition() isolates the extension.
  3. Mirror layouts. Without a match, rpartition() puts the data last, while partition() would put it first.

Example 3 — Split a GPS Sentence from Its Checksum

NMEA sentences from GPS receivers end with * and a two-digit hex checksum. Split at the last star and convert the checksum.

python
sentence = b"GPGGA,123519,4807.038,N*47"

body, star, checksum = sentence.rpartition(b"*")
print(body)
print(checksum, int(checksum, 16))

How It Works

  1. Trailer at the end. The checksum always follows the last star, so rpartition() is the natural split.
  2. Body for checking. The part before the star is what the checksum is computed over.
  3. Hex bytes to int. int() accepts ASCII bytes with a base, so b'47' in base 16 is 71.

Frequently Asked Questions

(b"", b"", data). The data is in the last position.
When the item you need comes after the last separator, such as an extension, a port, or a checksum.
rpartition() always returns three items including the separator; rsplit(sep, 1) returns a list of one or two items.
Yes. It always has exactly three items: before, sep, after = data.rpartition(b":").
No. Use a bytes separator such as b":".

Next: bytes.rsplit()

Split bytes from the right, so maxsplit keeps the leftmost part together.

rsplit() →

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.

10 people found this page helpful