Python bytes.rsplit() Method

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

Definition

bytes.rsplit() returns a list of bytes objects, just like split(), but it makes its cuts starting from the right.

Without maxsplit the result is identical to split(). With maxsplit, the rightmost fields are separated and everything to the left stays in one piece, which is exactly what you want for extensions, last path components, and trailing values.

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

Syntax

Same arguments as split().

python
parts = data.rsplit()
parts = data.rsplit(sep)
parts = data.rsplit(sep, maxsplit)
CallWhat it does
b"a.b.c".rsplit(b".", 1)Returns [b'a.b', b'c'].
b"a.b.c".split(b".", 1)Returns [b'a', b'b.c'] for comparison.
b"a b c".rsplit(None, 1)Returns [b'a b', b'c'].

The list is still in left-to-right order; only the choice of which separators to cut changes.

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

Parameters

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

Return Value

Return ValueDescription
listA list of bytes objects in their original order.

Python Examples

Example 1 — Split a Path at the Last Dot and Slash

A log path has dots in the file name. rsplit() with maxsplit=1 isolates just the final extension or file name.

python
path = b"logs/2026/app.error.log"

print(path.rsplit(b".", 1))
print(path.split(b".", 1))
print(path.rsplit(b"/", 1)[-1])

How It Works

  1. Last dot. One cut from the right separates only the final extension.
  2. split() differs. One cut from the left happens at the first dot instead.
  3. Last component. Taking item -1 after one right split gives the file name.

Example 2 — rsplit() in Whitespace Mode

Parse a command whose last token is the one you need, and compare which side keeps the extra whitespace.

python
cmd = b"  move  10 20  "

print(cmd.rsplit())
print(cmd.rsplit(None, 1))
print(cmd.split(None, 1))

How It Works

  1. No limit. Without maxsplit the result equals split(): three tokens, no empty pieces.
  2. Right side first. One cut from the right takes the last token; the left part keeps its leading spaces.
  3. Mirror image. split(None, 1) takes the first token and leaves the trailing spaces on the rest.

Example 3 — rsplit() vs split() with maxsplit=2

Without a limit both methods agree; with a limit they keep different ends together.

python
record = b"a:b:c:d"

print(record.rsplit(b":") == record.split(b":"))
print(record.rsplit(b":", maxsplit=2))
print(record.split(b":", maxsplit=2))

How It Works

  1. Same without a limit. Every separator is cut either way, so the lists are equal.
  2. Two cuts from the right. c and d are separated, while a:b stays together.
  3. Two cuts from the left. split() keeps the right end, c:d, together instead.

Frequently Asked Questions

Only when you pass maxsplit. Without it, both return the same list.
data.rsplit(sep, 1)[-1] returns the part after the last separator (or the whole data if there is none).
No. The pieces are in normal left-to-right order.
Yes: data.rsplit(b":", maxsplit=1).
rsplit() returns a list of one or two items; rpartition() always returns a 3-tuple including the separator.

Next: bytes.rstrip()

Return a copy with trailing bytes from a set removed (whitespace by default).

rstrip() →

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.

11 people found this page helpful