Python bytes.removesuffix() Method

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

Definition

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)
CallWhat 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

ParameterDescription
suffixThe exact bytes to remove from the end. Must be bytes-like.

Return Value

Return ValueDescription
bytesThe 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"))

How It Works

  1. Outer extension. .gz is removed first, leaving the tar name.
  2. Inner extension. A second call on the result removes .tar.
  3. 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().

python
reply = b"OK\r\n\r\n"

print(reply.removesuffix(b"\r\n"))
print(reply.rstrip(b"\r\n"))
print(b"OK".removesuffix(b"\r\n"))

How It Works

  1. Exactly one pair. removesuffix() cuts a single CRLF and keeps the line's own ending.
  2. Everything goes. rstrip() keeps removing CR and LF bytes until K stops it.
  3. 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")

How It Works

  1. Check, then cut. endswith() confirms the trailer before it is removed.
  2. Body only. The seven payload bytes remain.
  3. Type follows the object. On a bytearray the result is a bytearray.
  4. 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.

Next: bytes.replace()

Return a copy with occurrences of one byte sequence replaced by another.

replace() →

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.

14 people found this page helpful