The removeprefix() method of bytearray class in Python

The removeprefix() method from the bytearray class in Python removes a given prefix which is any bytes-like object and returns a new bytearray.

Bytearray Updated 14 Nov 2025 1 min read

Overview

The removeprefix() method of bytearray in Python

  • The removeprefix() method of bytearray class in Python removes a given prefix from a bytearray object. The method returns the result as new bytearray.
  • The prefix parameter can be any bytes-like object including a bytes literal.

Example

python
# Example Python program that removes the specified

            # prefix in a bytearray

# Create a bytearray

            buffer = bytearray(b"Well, now that we have seen each other")

            prefix = b"Well, "

# Remove the prefix

            prefixRemoved = buffer.removeprefix(prefix)

            print("The prefix:")

            print(prefix)

print("Byte array after removing the prefix:")

            print(prefixRemoved)

Output

Output
The prefix:

            b'Well, '

            Byte array after removing the prefix:

            bytearray(b'now that we have seen each other')