Bytearray Methods

bytearray.removesuffix()

Remove one exact matching suffix from the corresponding edge of the data. If the candidate is absent or empty, preserve all byte values. Unlike rstrip(), 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.removesuffix(suffix, /)

Parameters & Attributes

NameTypeDescription
suffixbytes-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').removesuffix(b'-end'))print(bytearray(b'payload').removesuffix(b'none'))print(bytearray(b'abc').removesuffix(b''))
Python Reference