bytes.strip()

The argument is a SET of bytes to remove, not a prefix to match. That one misunderstanding accounts for nearly every strip bug ever written.

Bytes methodPython 3.0+Live demo
Common call
data.strip()
Returns
a new bytes object with the ends trimmed
Replaces
manual index hunting at both ends
Watch out
strip(b'ab') removes any a and b, not the sequence 'ab'
bytes.strip([chars])
→ bytes

Demo

Live evaluation
Try:
Inputs
sstrdata (encoded as utf-8)
charsstrbyte values to strip
Output
bytes('xxabxx', 'utf-8').strip(bytes('x', 'utf-8'))
b'ab'

The second case is the one to study. Stripping "ab" from "abcba" gives "c" — it removed a, then b from the front, and a, then b from the back, because the argument is a SET of bytes to keep removing while they match. It is not a prefix or suffix. The middle case shows the other half of the rule: only the ends are considered, so an x between other bytes survives.

Parameters

NameTypeRequiredDescription
charsbytesno (None)A SET of byte values to strip from both ends. Omitted or None strips ASCII whitespace: space, tab, newline, carriage return, vertical tab and form feed.

Return value

bytes — A new bytes object with leading and trailing bytes removed. Only the ends are touched; the middle is untouched.

Common patterns

Trim a line read in binary mode
The default form removes CR and LF together, which handles both line-ending styles.
clean = raw_line.strip()
Strip padding bytes
Fixed-width binary fields are often null-padded.
value = field.strip(b'\x00')
Trim one end only
lstrip and rstrip take the same set argument.
data.rstrip(b'\n')

Examples

1. Both ends
b'xxabxx'.strip(b'x')
Returns
b'ab'
2. Set, not sequence
b'abcba'.strip(b'ab')
Returns
b'c'
3. Default whitespace
b' ab '.strip()
Returns
b'ab'
4. Nothing matches
b'abc'.strip(b'z')
Returns
b'abc'
5. Middle survives
b'xaxbx'.strip(b'x')
Returns
b'axb'
6. Everything goes
b'aaa'.strip(b'a')
Returns
b''

Pitfalls

1. chars is a SET, not a prefix
The most common misunderstanding in the whole language. strip keeps removing any byte that appears in the argument, so passing a word removes its letters individually from both ends.
Removes letters
b'testfile.txt'.strip(b'.txt')
b'estfile' # t, x and . all stripped
Use removesuffix
b'testfile.txt'.removesuffix(b'.txt')
b'testfile'
2. It only touches the ends
Bytes in the middle are never removed, however many times they appear in the set. Reaching for strip to delete a byte everywhere silently leaves the interior untouched.
Middle kept
b'xaxbx'.strip(b'x')
b'axb'
Use replace
b'xaxbx'.replace(b'x', b'')
b'ab'
3. The argument must be bytes
A str raises, like every bytes method. The default no-argument form is fine, which is why this often surfaces only once someone adds an explicit set.
str rejected
b'xxa'.strip('x')
TypeError: a bytes-like object is required, not 'str'
Bytes literal
b'xxa'.strip(b'x')
b'a'
4. It returns a new object
Bytes are immutable, so the result must be assigned. Calling strip for its supposed side effect does nothing.
Result dropped
data.strip()
data
unchanged
Assign it
data = data.strip()
trimmed

When to use

Use it
  • Trimming whitespace or line endings from binary-mode input
  • Removing padding bytes from fixed-width fields
  • Cleaning the ends of a token before parsing it
Reach for something else
  • Removing a known prefix or suffix → removeprefix and removesuffix
  • Removing a byte everywhere → replace
  • The data is text → decode first and use str.strip

Notes

Complexity
O(n) worst case, but usually only a few bytes at each end
Return
A new bytes object; CPython may return the original when nothing was trimmed
CPython impl
Objects/bytesobject.c :: bytes_strip
Memory
Allocates the trimmed result
Thread-safe
Yes — bytes are immutable

FAQ

Because it strips the SET of bytes dot, t, x — not the suffix. Any of those at either end is removed, repeatedly, until something else appears. removesuffix is what you actually wanted.

b'testfile.txt'.removesuffix(b'.txt')

History

3.0
bytes.strip arrived with the bytes type in the text/binary split.
3.9
removeprefix and removesuffix added, giving the exact-match behaviour people often wanted from strip.