all Python 3Byte sequencesfunction
bytes.strip
bytes — stripping
Strip given bytes (whitespace by default) from both ends.
Syntax
b.strip(...)
The argument is a set of byte values, not a substring: bytes are dropped from each end as long as they belong to that set, which is why b'banana'.strip(b'ba') gives b'nan'. To remove a fixed head or tail, reach for removeprefix()/removesuffix(). Nothing is modified in place — both bytes and bytearray return a new object.
Examples
print(b' hi '.strip()) # → b'hi'
print(b'xxhixx'.strip(b'x')) # → b'hi'
print(b'\t hi \n'.strip()) # → b'hi'
print(b'banana'.strip(b'ba')) # → b'nan'
print(b' hi '.rstrip()) # → b' hi'
print(b' '.strip()) # → b''
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy