Bytes Methods

bytes.removeprefix()

Remove one exact matching prefix from the corresponding edge of the data. If the candidate is absent or empty, preserve all byte values. Unlike lstrip(), this treats the argument as an ordered sequence, not a collection of removable byte values. Introduced in Python 3.9. Returns bytes; no distinct identity guarantee.

Formal Syntax & Structure

python syntaxpython
data.removeprefix(prefix, /)

Parameters & Attributes

NameTypeDescription
prefixbytes-likeExact binary sequence to remove once if present.

Return Value

bytes

bytes with one matching edge removed, or unchanged byte values.

Working Example

Example Snippetpython
print(bytes(b'pre-pre-data-end-end').removeprefix(b'pre-'))print(bytes(b'payload').removeprefix(b'none'))print(bytes(b'abc').removeprefix(b''))
Python Reference