Python bytes.rstrip() Method

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

Definition

bytes.rstrip() returns a new bytes object with bytes removed from the right end only. With no argument it removes ASCII whitespace; with chars it removes any byte that appears in that set.

It is the everyday way to drop \r\n from lines read in binary mode and to trim the zero bytes that pad fixed-size name fields in binary file formats.

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

Syntax

Strip trailing whitespace, or a set of bytes you choose.

python
result = data.rstrip()
result = data.rstrip(chars)
CallWhat it does
b"line\r\n".rstrip()Returns b'line'.
b"name\x00\x00".rstrip(b"\x00")Returns b'name'.
b" hi ".rstrip()Returns b' hi': leading spaces stay.

rstrip(b"\r\n") removes any mix of CR and LF bytes at the end, not only one CRLF pair.

Method signatures
python
bytes.rstrip(chars=None, /)
# chars: bytes-like set of byte values; None means ASCII whitespace

Parameters

ParameterDescription
charsOptional. The set of byte values to remove from the right. Default None removes ASCII whitespace.

Return Value

Return ValueDescription
bytesA copy without the trailing bytes that belong to the set.

Python Examples

Example 1 — Drop Line Endings, Keep Other Spaces

Compare stripping only CR and LF with stripping all whitespace on lines read in binary mode.

python
lines = [b"alpha\r\n", b"beta\n", b"gamma  \n"]

print([line.rstrip(b"\r\n") for line in lines])
print([line.rstrip() for line in lines])

How It Works

  1. Only line endings. With b"\r\n" as the set, CRLF and LF are removed but the spaces after gamma survive.
  2. All whitespace. The default call also removes the trailing spaces.
  3. Pick deliberately. Trailing spaces can be meaningful in some formats, so choose the narrower set when unsure.

Example 2 — Read a Null-Padded Name Field

Binary formats often store names in a fixed 16-byte field filled with zero bytes. Strip the padding and decode the name.

python
record = b"README.TXT\x00\x00\x00\x00\x00\x00"

name = record.rstrip(b"\x00")
print(len(record), len(name))
print(name, name.decode("ascii"))

How It Works

  1. Fixed field. The record is always 16 bytes: a 10-byte name plus 6 zero bytes.
  2. Trim the padding. Only the trailing zeros are removed, leaving the real name.
  3. To text. decode() with the ASCII codec turns the name into a str.

Example 3 — rstrip() Is Not a Suffix Remover

Stripping an extension with a set removes one letter too many. An endswith() check plus slicing is exact, and chaining two strips tidies a decimal.

python
name = b"report.txt"

print(name.rstrip(b".txt"))
if name.endswith(b".txt"):
    print(name[:-len(b".txt")])
print(b"9.500".rstrip(b"0").rstrip(b"."))

How It Works

  1. One letter too many. After removing .txt, the final t of report is also in the set, so it goes too.
  2. Exact removal. endswith() confirms the suffix and the slice drops exactly four bytes.
  3. Useful chaining. Stripping trailing zeros and then a trailing dot formats 9.500 as 9.5.

Frequently Asked Questions

Use line.rstrip(b"\r\n") to remove only CR and LF, or line.rstrip() to remove all trailing whitespace.
Use field.rstrip(b"\x00").
The argument is a set of bytes. Use removesuffix(b".txt") on Python 3.9+ to remove the exact ending.
No. Only trailing bytes are removed.
bytes. Call decode() afterwards if you need a str.

Next: bytes.split()

Split bytes into a list of bytes using a separator or runs of whitespace.

split() →

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