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
| Parameter | Description |
|---|---|
suffix |
The exact text to remove from the end. |
Return value
A new string.
Examples
print("report.pdf".removesuffix(".pdf"))
print("report.txt".removesuffix(".pdf"))
Output
report report.txt
print("logs.tar.gz".removesuffix(".gz").removesuffix(".tar"))
Output
logs
Version history
| Version | Change |
|---|---|
| Python 3.9 | Added |
Related
str.removeprefix()· Return the string without the given prefix if it starts with it; otherwise return it unchanged.str.rstrip()· Return a copy with trailing characters removed: whitespace by default, or any of the characters you pass.str.endswith()· Return True if the string ends with the given suffix. Pass a tuple to check several suffixes at once.- Python Strings · tutorial