bytes.strip() returns a new bytes object with unwanted bytes removed from both ends. With no argument it removes ASCII whitespace: space, \t, \n, \r, \x0b, and \x0c.
Pass chars to choose which bytes to remove. It is treated as a set of byte values, not as a word: stripping stops at the first byte on each side that is not in the set. Bytes in the middle are never touched.
bytes.strip() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.strip(...). No import is needed, and bytearray objects have the same method.
Syntax
Strip whitespace, or a set of bytes you choose.
python
result = data.strip()
result = data.strip(chars)
Call
What it does
b" hi\r\n".strip()
Returns b'hi'.
b"\x00\x00ok\x00".strip(b"\x00")
Returns b'ok'.
b"xyhiyx".strip(b"xy")
Returns b'hi': x and y are removed in any order.
strip(None) is the same as strip().
Method signatures
python
bytes.strip(chars=None, /)
# chars: bytes-like set of byte values to remove; None means ASCII whitespace
Parameters
Parameter
Description
chars
Optional. A bytes-like object listing the byte values to remove from both ends. Default None removes ASCII whitespace.
Return Value
Return Value
Description
bytes
A copy without the leading and trailing bytes that belong to the set.
Python Examples
Example 1 — Clean Lines Received from a Socket
Commands arrive with stray spaces, tabs, and line endings. The default strip removes all ASCII whitespace from both ends.
python
lines = [b" PING\r\n", b"\tPONG \x0b", b"\x00DATA\x00"]
for raw in lines:
print(raw.strip())
Output
b'PING'
b'PONG'
b'\x00DATA\x00'
How It Works
Both ends cleaned. Leading spaces and the trailing CRLF are removed from the first line.
Vertical tab counts.\x0b is ASCII whitespace too, so it disappears along with the tab and space.
Null is not whitespace. The zero bytes around DATA stay; they need an explicit chars argument.
Example 2 — Trim Padding Bytes with a Set
Binary fields are often padded with zero bytes, and text may be wrapped in decoration. Pass the bytes to remove as a set.
python
field = b"\x00\x00ID42\x00\x00\x00"
print(field.strip(b"\x00"))
print(b"xyhixyx".strip(b"xy"))
print(b"<<data>>".strip(b"<>"))
Output
b'ID42'
b'hi'
b'data'
How It Works
Null padding. Two zero bytes in front and three behind are removed, leaving the ID.
Any order. The ends contain x and y in mixed order; both belong to the set, so all of them go.
Both brackets. One argument covers < on the left and > on the right.
Example 3 — strip() Takes a Set, Not a Suffix
Trying to strip a file extension shows why chars is not a substring, and a str argument shows the type rule.
python
name = b"bob.bin"
print(name.strip(b".bin"))
try:
name.strip(".bin")
except TypeError:
print("chars must be bytes-like")
Output
b'o'
chars must be bytes-like
How It Works
Four separate bytes.b".bin" means the set {., b, i, n}, not the extension.
Eaten from both sides. The leading b goes, and on the right n, i, b, ., and b are all removed until o stops it.
Wrong type. A str set raises TypeError; bytes methods never accept text arguments.