Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

str.removesuffix

Removes one exact suffix when it is present.

Version: Python 3.14; repository image, production runtime not attested

Syntax

s.removesuffix(suffix)

Removes one exact suffix when it is present.

An empty suffix leaves the entire string intact, unlike slicing up to negative zero.

One trailing .tar is removed from a.tar.tar, retaining a.tar. An empty suffix preserves all text. Slicing to -0 would instead stop at index zero and produce an empty string.

assert "abc".removesuffix("") == "abc" and "abc"[:-0] == ""

Examples

print("a.tar.tar".removesuffix(".tar"))
print("a.tar".removesuffix(""))

Result

a.tar
a.tar

One trailing .tar is removed from a.tar.tar, retaining a.tar. An empty suffix preserves all text. Slicing to -0 would instead stop at index zero and produce an empty string.

Sources