all Python 3Byte sequencesfunction
bytearray.rstrip
bytearray — stripping
Return a copy with the given bytes removed from the right; by default strips ASCII whitespace.
Syntax
ba.rstrip(...)
The argument is a set of bytes, not a suffix: rstrip(b'an') keeps removing trailing 'a' and 'n' bytes, so b'banana' collapses to b'b'. If you mean "drop this exact ending", use removesuffix() (3.9+). The call returns a new bytearray and leaves the original as it was.
Examples
print(bytearray(b'hi ').rstrip()) # → bytearray(b'hi')
print(bytearray(b'file.txt...').rstrip(b'.')) # → bytearray(b'file.txt')
print(bytearray(b'xxhixx').rstrip(b'x')) # → bytearray(b'xxhi')
print(bytearray(b'banana').rstrip(b'an')) # → bytearray(b'b')
print(bytearray(b' hi ').strip()) # → bytearray(b'hi')
See also
Open in the full glossarydocs.python.orgReport an inaccuracy