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.
Parameters
| Name | Description |
|---|---|
| suffix | The exact ending text to remove. |
Returns
A new string with the suffix removed, or the original text if it does not end with that suffix.
Try it
Run it and change it
filename = "report.txt"
print(filename.removesuffix(".txt"))
Worth knowing
- Safe file extension trimming: Ideal for safely stripping file extensions like
filename.removesuffix('.py').