Python Glossary Русский

Python 3.9+Byte sequencesfunction

bytearray.removesuffix

bytearray — stripping

Remove a given suffix if present (3.9+).

Syntax

ba.removesuffix(...)

It strips exactly one occurrence of the whole given sequence, unlike rstrip(b'.txt'), which keeps chewing off any of the bytes '.', 't' and 'x'. When the suffix is absent you simply get an unchanged copy — and either way the original bytearray is untouched, since this is not an in-place operation.

Examples

print(bytearray(b'foobar').removesuffix(b'bar'))   # → bytearray(b'foo')
print(bytearray(b'foobar').removesuffix(b'foo'))   # → bytearray(b'foobar')
print(bytearray(b'report.txt.txt').removesuffix(b'.txt'))   # → bytearray(b'report.txt')
ba = bytearray(b'file.csv'); print(ba.removesuffix(b'.csv'), ba)   # → bytearray(b'file') bytearray(b'file.csv')
print(bytearray(b'abc').removesuffix(b''))   # → bytearray(b'abc')

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy