Python bytes.strip() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
bytes method

Definition

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)
CallWhat 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

ParameterDescription
charsOptional. A bytes-like object listing the byte values to remove from both ends. Default None removes ASCII whitespace.

Return Value

Return ValueDescription
bytesA 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())

How It Works

  1. Both ends cleaned. Leading spaces and the trailing CRLF are removed from the first line.
  2. Vertical tab counts. \x0b is ASCII whitespace too, so it disappears along with the tab and space.
  3. 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"<>"))

How It Works

  1. Null padding. Two zero bytes in front and three behind are removed, leaving the ID.
  2. Any order. The ends contain x and y in mixed order; both belong to the set, so all of them go.
  3. 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")

How It Works

  1. Four separate bytes. b".bin" means the set {., b, i, n}, not the extension.
  2. Eaten from both sides. The leading b goes, and on the right n, i, b, ., and b are all removed until o stops it.
  3. Wrong type. A str set raises TypeError; bytes methods never accept text arguments.

Frequently Asked Questions

ASCII whitespace: space, \t, \n, \r, \x0b (vertical tab), and \x0c (form feed).
Pass them as the set: data.strip(b"\x00").
The argument is a set of bytes. Any t, x, or . at either end is removed. Use removesuffix(b".txt") on Python 3.9+.
No. It only works inward from both ends and stops at the first byte that is not in the set.
No. It returns a new bytes object.

Next: bytes.swapcase()

Return a copy with ASCII uppercase and lowercase letters swapped.

swapcase() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful