Python 3 reference · String Methods

Python String removesuffix() Method

Return the string without the given suffix if it ends with it; otherwise return it unchanged.

Python 3 Since 3.9

Syntax

str.removesuffix(suffix)

Parameters

ParameterDescription
suffix The exact text to remove from the end.

Return value

A new string.

Examples

Remove a suffix if presentTry it yourself
print("report.pdf".removesuffix(".pdf"))
print("report.txt".removesuffix(".pdf"))

Output

report
report.txt
Chain themTry it yourself
print("logs.tar.gz".removesuffix(".gz").removesuffix(".tar"))

Output

logs

Version history

VersionChange
Python 3.9Added