Python removesuffix() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

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.

python
clean = text.removesuffix(suffix)
CallWhat 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
python
str.removesuffix(suffix) -> str
# returns: a new string without the suffix, or an unchanged copy
# added in Python 3.9

Parameters

ParameterDescription
suffixRequired. The exact text to remove from the end. It must be a string. An empty suffix removes nothing.

Return Value

Return ValueDescription
strA new string without the suffix if the string ended with it, otherwise a string equal to the original. The original is never changed.

Python Examples

Example 1 — Drop a File Extension

Remove an extension that is there, one that is missing, and see that only one copy is removed.

python
name = "report.txt"

print(name.removesuffix(".txt"))
print(name.removesuffix(".csv"))
print("doneyyy".removesuffix("y"))
print(name)

How It Works

  1. An exact match. The name ends with .txt, so those four characters are removed.
  2. No match, no change. .csv is not at the end, so the same text comes back.
  3. Only once. "doneyyy" loses a single y, not all three.
  4. 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.

python
name = "context.txt"

print(name.rstrip(".txt"))
print(name.removesuffix(".txt"))
print(name.rstrip(".txt") == name.removesuffix(".txt"))

How It Works

  1. 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.
  2. removesuffix() is exact. It removes only the four characters .txt at the end and keeps context.
  3. 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.

python
links = ["https://a.dev/", "https://b.dev", "https://c.dev//"]
clean = [link.removesuffix("/") for link in links]

print(clean)

How It Works

  1. One call for each link. The comprehension runs removesuffix("/") on every address.
  2. A link without a slash stays. https://b.dev does not end with a slash, so it is copied unchanged.
  3. Only one slash goes. The double slash of the third link loses its last character only.

Frequently Asked Questions

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.
Use name.removesuffix(".txt") in Python 3.9 and later. For older versions check name.endswith(".txt") and slice with name[:-4].
removesuffix() removes one exact piece of text. rstrip() removes any characters from a set, as many times as they appear at the end.
Python 3.9, together with removeprefix().
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.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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