Python Glossary Русский

all Python 3Byte sequencesfunction

bytes.rstrip

bytes — stripping

Strip given bytes from the right.

Syntax

b.rstrip(...)

The usual use is dropping b'\r\n' from a line read out of a file or socket. But bare rstrip() also eats meaningful trailing spaces and tabs, and rstrip(b'\r\n') removes every repetition of those bytes, not a single line ending. When exactly one known tail must go, use removesuffix().

Examples

print(b'hi  '.rstrip())   # → b'hi'
print(b'  hi  '.rstrip())   # → b'  hi'
print(b'42\r\n'.rstrip())   # → b'42'
print(b'xxhixx'.rstrip(b'x'))   # → b'xxhi'
print(b'text.txt'.rstrip(b'.txt'))   # → b'te'

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy