str.removesuffix
Remove the given ending from the string if it is there.
Python 3.13
str.removesuffix(suffix)Parameters
- suffixstr
- The ending of the string to remove.
Return value
A new string; the original is not changed.
Examples
Removing a file extension
Python 3.13
print('report.txt'.removesuffix('.txt'))report
Difference from rstrip
Python 3.13
print('test.txt'.removesuffix('.txt'), 'test.txt'.rstrip('.txt'))test tes
Details
Unlike str.rstrip, it removes exactly the substring, not a set of characters. If the string ends differently, it is returned unchanged. The method was added in Python 3.9.
