Python bytes.removeprefix() Method

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

Definition

bytes.removeprefix(prefix) checks whether the data starts with prefix. If it does, it returns a copy with that prefix cut off once; otherwise it returns the data unchanged.

It replaces the common two-step pattern of startswith() followed by slicing, and it avoids the set behavior of lstrip(), which removes individual bytes rather than an exact sequence.

bytes.removeprefix() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.removeprefix(...). No import is needed, and bytearray objects have the same method. New in Python 3.9.

Syntax

Pass the exact byte sequence to remove from the start (Python 3.9+).

python
result = data.removeprefix(prefix)
CallWhat it does
b"HTTP/1.1".removeprefix(b"HTTP/")Returns b'1.1'.
b"abc".removeprefix(b"x")Returns b'abc': no match, no change.
b"aab".removeprefix(b"a")Returns b'ab': removed only once.

The match is exact and case-sensitive.

Method signatures
python
bytes.removeprefix(prefix, /)
# new in Python 3.9; prefix: bytes-like object

Parameters

ParameterDescription
prefixThe exact bytes to remove from the start. Must be bytes-like.

Return Value

Return ValueDescription
bytesdata[len(prefix):] when the data starts with prefix, otherwise a copy of the data.

Python Examples

Example 1 — Remove a Protocol Prefix from a Status Line

Strip HTTP/ to get the version number, and see that a wrong or wrongly-cased prefix changes nothing.

python
status = b"HTTP/1.1 200 OK"

print(status.removeprefix(b"HTTP/"))
print(status.removeprefix(b"FTP/"))
print(status.removeprefix(b"http/"))

How It Works

  1. Exact prefix removed. The five bytes HTTP/ are cut from the front.
  2. Different prefix. The line does not start with FTP/, so it is returned unchanged.
  3. Case-sensitive. Lowercase http/ does not match either.

Example 2 — Drop a UTF-8 Byte Order Mark

CSV files exported by some editors start with the three-byte UTF-8 BOM. Remove it before parsing; calling again is harmless.

python
BOM = b"\xef\xbb\xbf"
csv_bytes = BOM + b"id,name\n1,Ada\n"

clean = csv_bytes.removeprefix(BOM)
print(clean[:7])
print(clean.removeprefix(BOM) == clean)
print(len(csv_bytes) - len(clean))

How It Works

  1. Three marker bytes. The BOM is \xef\xbb\xbf, placed before the header row.
  2. Clean header. After removal, the data starts directly with id,name.
  3. Safe to repeat. A second call finds no BOM and returns equal bytes.
  4. Exactly three bytes. The length difference confirms only the BOM was removed.

Example 3 — Removed Once, Unlike lstrip()

Compare removeprefix() with lstrip() on repeated bytes, try an empty prefix, and call it on a bytearray.

python
data = b"ababc"

print(data.removeprefix(b"ab"))
print(data.lstrip(b"ab"))
print(data.removeprefix(b""))
print(bytearray(b"--x").removeprefix(b"-"))

How It Works

  1. One occurrence. Only the first ab is removed, even though it repeats.
  2. Set behavior. lstrip(b"ab") removes every leading a and b byte, leaving only c.
  3. Empty prefix. Removing nothing returns equal bytes.
  4. bytearray result. One hyphen is removed and the type stays bytearray.

Frequently Asked Questions

Python 3.9. It raises AttributeError on older versions.
removeprefix() removes one exact sequence once. lstrip() removes any leading bytes from a set, as many as it finds.
The data is returned unchanged; no exception is raised.
Use data[len(p):] if data.startswith(p) else data.
No. It takes a single bytes-like prefix. Loop over candidates and use startswith() to pick one.

Next: bytes.removesuffix()

Remove an exact suffix once if present (Python 3.9+).

removesuffix() →

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.

13 people found this page helpful