Python - remove a suffix from a string


Python tip (>=3.9):

You can remove the suffix of a string with .removesuffix().

For example, to remove the file type from a filename:

import pathlib

filename = "cv.pdf"

file_type_suffix = pathlib.Path(filename).suffix
print(filename.removesuffix(file_type_suffix))
# => cv