Python 3.9+Byte sequencesfunction
bytes.removesuffix
bytes — stripping
Remove a given suffix if present (3.9+).
Syntax
b.removesuffix(...)
The suffix is compared as a whole and removed exactly once; if it does not match, the value is returned unchanged rather than raising. For cutting an extension or a line ending this is safer than rstrip(), which treats its argument as a set of bytes and will happily eat more than one trailing byte.
Examples
print(b'foobar'.removesuffix(b'bar')) # → b'foo'
print(b'report.txt'.removesuffix(b'.txt')) # → b'report'
print(b'report.txt'.removesuffix(b'.csv')) # → b'report.txt'
print(b'line\n'.removesuffix(b'\n')) # → b'line'
print(b'banana'.removesuffix(b'na')) # → b'bana'
print(b'banana'.rstrip(b'na')) # → b'b'
See also
Open in the full glossarydocs.python.orgReport an inaccuracy