bytes.rstrip()
The other half of strip, and the one used most — dropping the newline from a line read in binary mode while keeping any leading indentation.
Common call
line.rstrip(b'\r\n')
Returns
a new bytes object with the right end trimmed
Replaces
checking for and slicing off a line terminator
Watch out
rstrip(b'.txt') strips the SET of bytes, not the suffix
bytes.rstrip([chars])
→ bytes
Demo
Live evaluation
Try:
Inputs
sstrdata (encoded as utf-8)
charsstrbyte values to strip
Output
bytes('xxabxx', 'utf-8').rstrip(bytes('x', 'utf-8'))
b'xxab'
Only the right end is touched — xxabxx becomes xxab, with the leading x bytes kept. The set rule applies as with strip: removing "ab" from "abcba" takes a then b off the end and stops at c, leaving abc. Everything in the set is removed repeatedly from the right until a byte outside it appears.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| chars | bytes | no (None) | A SET of byte values to strip from the right. Omitted or None strips ASCII whitespace. |
Return value
bytes — A new bytes object with trailing bytes removed. The left end is untouched.
Common patterns
Drop a line ending
Handles both CRLF and LF, keeping leading indentation.
line = raw.rstrip(b'\r\n')
Remove trailing padding
Space or null padded fields from fixed-width formats.
value = field.rstrip(b'\x00')
Remove an exact suffix instead
For a specific sequence, removesuffix is the tool.
name.removesuffix(b'.txt')
Examples
1. Right only
b'xxabxx'.rstrip(b'x')
Returns
b'xxab'2. Set, not sequence
b'abcba'.rstrip(b'ab')
Returns
b'abc'3. Line ending
b'line\r\n'.rstrip(b'\r\n')
Returns
b'line'4. Default whitespace
b' ab '.rstrip()
Returns
b' ab'5. Everything goes
b'aaa'.rstrip(b'a')
Returns
b''6. Empty
b''.rstrip(b'x')
Returns
b''Pitfalls
1. chars is a SET, not a suffix
Passing an extension strips its letters one at a time from the right. A filename ending in any of those letters loses them too.
Letters removed
b'testfile.txt'.rstrip(b'.txt')
b'testfile' # and b'text.txt' would become b'te'
Use removesuffix
b'text.txt'.removesuffix(b'.txt')
b'text'
2. It only touches the right end
Leading bytes in the set survive. To clean both ends use strip.
Left kept
b'xxabxx'.rstrip(b'x')
b'xxab'
Use strip
b'xxabxx'.strip(b'x')
b'ab'
3. The argument must be bytes
A str raises. Text-oriented code pointed at a binary-mode file hits this on the first explicit argument.
str rejected
b'ab\n'.rstrip('\n')
TypeError: a bytes-like object is required, not 'str'
Bytes literal
b'ab\n'.rstrip(b'\n')
b'ab'
When to use
Use it
- Dropping line endings from binary-mode input
- Removing trailing padding from fixed-width fields
- Cleaning the end while preserving leading indentation
Reach for something else
- Removing a known suffix → removesuffix
- Cleaning both ends → strip
- Line splitting → splitlines handles terminators itself
Notes
Complexity
O(n) worst case, usually a few bytes at the right end
Return
A new bytes object; the original when nothing was trimmed
CPython impl
Objects/bytesobject.c :: bytes_rstrip
Memory
Allocates the trimmed result
Thread-safe
Yes — bytes are immutable
FAQ
Because it strips the SET of bytes dot, t and x from the right, repeatedly. On b"text.txt" it removes .txt and then keeps going through the t of "text". removesuffix matches the exact sequence once.
b'text.txt'.rstrip(b'.txt') # b'te' b'text.txt'.removesuffix(b'.txt') # b'text'
History
3.0
bytes.rstrip arrived with the bytes type in the text/binary split.