Bytearray Methods

bytearray.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. Always returns a new bytearray, even when unchanged.

Formal Syntax & Structure

python syntaxpython
data.removeprefix(prefix, /)

Parameters & Attributes

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

Return Value

bytearray

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

Working Example

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