String Methods
removesuffix()
Remove one exact suffix if it is present.
str.removesuffix(suffix, /)
str.removesuffix() (Python 3.9+) returns a copy of the string with the specified exact suffix removed if present.
Arguments
Parameters
| Name | Description |
|---|---|
| suffix | The exact ending text to remove. |
Result
Returns
A new string with the suffix removed, or the original text if it does not end with that suffix.
Run it
Try it
Run it and change it
filename = "report.txt"
print(filename.removesuffix(".txt")) Output
Press Run to see what it prints.Worth knowing
- Safe file extension trimming: Ideal for safely stripping file extensions like
filename.removesuffix('.py').