str.removesuffix()
Added in v3.9 · Updated March 13, 2026 · String Methods
stdlib string methods
The .removesuffix() method returns a copy of the string with the suffix removed, if the string actually ends with that suffix. If the suffix is not present, the method returns a copy of the original string unchanged. This method was added in Python 3.9 as part of PEP 616, which introduced complementary methods for removing prefixes and suffixes.
Signature
str.removesuffix(suffix)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
suffix | str | Required | The suffix string to remove from the end |
Return Value
Returns a new string with the suffix removed. The original string remains unchanged.
Basic Examples
Remove a matching suffix
text = "HelloWorld"
result = text.removesuffix("World")
print(result) # Output: Hello
Suffix not present returns unchanged string
text = "PythonProgramming"
result = text.removesuffix("Java")
print(result) # Output: PythonProgramming
Case-sensitive matching
text = "PythonProgramming"
result = text.removesuffix("programming")
print(result) # Output: PythonProgramming
Practical Use Cases
Cleaning file extensions
# Remove file extension
filename = "document.pdf"
name = filename.removesuffix(".pdf")
print(name) # Output: document
Handling version strings
# Remove common suffixes from version identifiers
versions = ["v1.0.0", "v2.1.3-beta", "v3.0.0-rc1"]
for version in versions:
print(version.removesuffix("-beta").removesuffix("-rc1"))
# Output:
# v1.0.0
# v2.1.3
# v3.0.0
Normalizing identifiers
# Remove common suffixes from database column names
columns = ["users_id", "orders_total", "products_name"]
for col in columns:
print(col.removesuffix("_id").removesuffix("_total").removesuffix("_name"))
# Output:
# users
# orders
# products
Chaining with other string methods
# Remove suffix then convert to title case
text = "hello_world"
result = text.removesuffix("_world").title()
print(result) # Output: Hello
Common Patterns
Working with empty suffix
Passing an empty string as the suffix returns a copy of the original string:
text = "Hello"
result = text.removesuffix("")
print(result) # Output: Hello
This behavior can be useful in generalized string processing functions where the suffix might be optional.
Combining with conditional logic
You can use the method with conditional checks when you need different behavior based on whether the suffix exists:
def process_filename(filename):
if filename.endswith(".tmp"):
return filename.removesuffix(".tmp")
return filename
print(process_filename("data.tmp")) # Output: data
print(process_filename("important.txt")) # Output: important.txt
Behavior Notes
- The method is case-sensitive. Use
.casefold()or.lower()first if you need case-insensitive suffix removal. - If the string does not end with the specified suffix, the original string is returned unchanged.
- The method always returns a new string—it never modifies the original in place.
- This method was introduced alongside
.removeprefix()in Python 3.9 to provide a cleaner alternative to string slicing for suffix removal.