bytes.removesuffix(suffix) checks whether the data ends with suffix. If so, it returns a copy without it, removed once; otherwise it returns the data unchanged.
It is the safe replacement for data[:-len(suffix)] after an endswith() check, and unlike rstrip() it never removes extra bytes that just happen to be in a set.
bytes.removesuffix() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.removesuffix(...). No import is needed, and bytearray objects have the same method. New in Python 3.9.
Syntax
Pass the exact byte sequence to remove from the end (Python 3.9+).
python
result = data.removesuffix(suffix)
Call
What it does
b"log.gz".removesuffix(b".gz")
Returns b'log'.
b"OK\r\n\r\n".removesuffix(b"\r\n")
Returns b'OK\r\n': only one CRLF.
b"abc".removesuffix(b"")
Returns b'abc'.
An empty suffix always leaves the data unchanged, avoiding the classic data[:-0] bug that returns empty bytes.
Method signatures
python
bytes.removesuffix(suffix, /)
# new in Python 3.9; suffix: bytes-like object
Parameters
Parameter
Description
suffix
The exact bytes to remove from the end. Must be bytes-like.
Return Value
Return Value
Description
bytes
The data without suffix if it ended with it, otherwise a copy of the data.
Python Examples
Example 1 — Peel Extensions One at a Time
A compressed archive name has two extensions. Remove them one by one, and see that a missing extension changes nothing.
python
name = b"archive.tar.gz"
step1 = name.removesuffix(b".gz")
print(step1)
print(step1.removesuffix(b".tar"))
print(name.removesuffix(b".zip"))
Output
b'archive.tar'
b'archive'
b'archive.tar.gz'
How It Works
Outer extension..gz is removed first, leaving the tar name.
Inner extension. A second call on the result removes .tar.
No match. The name does not end with .zip, so it comes back unchanged.
Example 2 — Remove One Protocol Terminator
A reply ends with two CRLF pairs, where the second one marks the end of the message. Remove exactly one and compare with rstrip().
Exactly one pair.removesuffix() cuts a single CRLF and keeps the line's own ending.
Everything goes.rstrip() keeps removing CR and LF bytes until K stops it.
Nothing to remove. Data without the terminator is returned unchanged.
Example 3 — Strip a Checksum Trailer After Checking It
Confirm the expected trailer with endswith(), remove it, try a bytearray, and see the str error.
python
frame = b"PAYLOAD*7A"
if frame.endswith(b"*7A"):
body = frame.removesuffix(b"*7A")
print(body, len(body))
print(bytearray(b"data.bak").removesuffix(b".bak"))
try:
frame.removesuffix("*7A")
except TypeError:
print("suffix must be bytes, not str")
Output
b'PAYLOAD' 7
bytearray(b'data')
suffix must be bytes, not str
How It Works
Check, then cut.endswith() confirms the trailer before it is removed.
Body only. The seven payload bytes remain.
Type follows the object. On a bytearray the result is a bytearray.
Text rejected. A str suffix raises TypeError.
Frequently Asked Questions
Python 3.9.
removesuffix() removes one exact sequence once; rstrip() removes any trailing bytes from a set, as many as it finds.
It is returned unchanged; there is no exception.
That needs a separate endswith() check, and with an empty suffix data[:-0] returns empty bytes. removesuffix() handles both cases.
Use data[:-len(s)] if s and data.endswith(s) else data.