Home Python String Methods removesuffix() Python removesuffix() Method Definition removesuffix() removes one exact piece of text from the end of a string, but only if the string really ends with it. "report.txt".removesuffix(".txt") is "report". If the suffix is not there, you get the same text back.
It is the safe replacement for rstrip() when you mean a word such as a file extension and not a set of characters. It removes the suffix once , never more. The method was added in Python 3.9 , and it returns a new string.
text.removesuffix() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.
Syntax Pass the suffix to remove. It is required, and it must be a string.
clean = text.removesuffix(suffix) Call What it does "report.txt".removesuffix(".txt")Returns 'report'. The exact suffix is removed. "report.txt".removesuffix(".csv")Returns 'report.txt'. The text does not end with .csv, so it is unchanged. "xxx".removesuffix("x")Returns 'xx'. Only one copy of the suffix is removed.
For the start of a string use removeprefix() . To strip a set of characters use rstrip() . To only test the end, use endswith() .
Library declarations str.removesuffix(suffix) -> str
# returns: a new string without the suffix, or an unchanged copy
# added in Python 3.9 Parameters Parameter Description suffixRequired. The exact text to remove from the end. It must be a string. An empty suffix removes nothing.
Return Value Return Value Description strA new string without the suffix if the string ended with it, otherwise a string equal to the original. The original is never changed.
Important
removesuffix() needs Python 3.9 or later. It removes the suffix only once , and only as a whole. For older versions write text[:-len(s)] if s and text.endswith(s) else text. Do not use rstrip(".txt") to remove an extension, because it removes a set of characters.
Python Examples 1. Remove a suffix 2. removesuffix() or rstrip() 3. Tidy a list of addresses Example 1 — Drop a File Extension Remove an extension that is there, one that is missing, and see that only one copy is removed.
name = "report.txt"
print(name.removesuffix(".txt"))
print(name.removesuffix(".csv"))
print("doneyyy".removesuffix("y"))
print(name) report
report.txt
doneyy
report.txt How It Works An exact match. The name ends with .txt, so those four characters are removed.No match, no change. .csv is not at the end, so the same text comes back.Only once. "doneyyy" loses a single y, not all three.The original is unchanged. The last line still prints report.txt.Example 2 — Why rstrip() Is Not the Same Remove .txt in both ways and compare the answers.
name = "context.txt"
print(name.rstrip(".txt"))
print(name.removesuffix(".txt"))
print(name.rstrip(".txt") == name.removesuffix(".txt")) How It Works rstrip() eats too much. The characters t, x, and . are all in the set, so it removes the end of the word context as well, and stops at the e.removesuffix() is exact. It removes only the four characters .txt at the end and keeps context.The two disagree. The comparison is False, which is why rstrip() is not safe for extensions.Example 3 — Remove a Trailing Slash From Addresses Remove the final slash from every address that has one.
links = ["https://a.dev/", "https://b.dev", "https://c.dev//"]
clean = [link.removesuffix("/") for link in links]
print(clean) ['https://a.dev', 'https://b.dev', 'https://c.dev/'] How It Works One call for each link. The comprehension runs removesuffix("/") on every address.A link without a slash stays. https://b.dev does not end with a slash, so it is copied unchanged.Only one slash goes. The double slash of the third link loses its last character only.Frequently Asked Questions What does removesuffix() do in Python? It returns a copy of the string without the given suffix if the string ends with it. If it does not, it returns the string unchanged.
How do I remove a file extension in Python? Use name.removesuffix(".txt") in Python 3.9 and later. For older versions check name.endswith(".txt") and slice with name[:-4].
What is the difference between removesuffix() and rstrip()? removesuffix() removes one exact piece of text. rstrip() removes any characters from a set, as many times as they appear at the end.
Which Python version added removesuffix()? Python 3.9, together with removeprefix().
Does removesuffix() change the original string? No. Strings are immutable, so it returns a new string. Assign the result to keep it.
Next: replace() Return a copy with every occurrence of a substring replaced by another one.
replace() tutorial → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion