strip doesn't mean "remove this substring". x.strip(y) treats y as a set of characters and strips any characters in that set from both ends of x.

On Python 3.9 and newer you can use the removeprefix and removesuffix methods to remove an entire substring from either side of the string:

url = 'abcdc.com'
url.removesuffix('.com')    # Returns 'abcdc'
url.removeprefix('abcdc.')  # Returns 'com'

The relevant Python Enhancement Proposal is PEP-616.

On Python 3.8 and older you can use endswith and slicing:

url = 'abcdc.com'
if url.endswith('.com'):
    url = url[:-4]

Or a regular expression:

import re
url = 'abcdc.com'
url = re.sub('\.com$', '', url)
Answer from Steef on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-string-removesuffix
Python String - removesuffix() - GeeksforGeeks
July 23, 2025 - This method returns a new string with the suffix removed. If the suffix is not found at the end, it returns the original string unchanged. removesuffix() method is case-sensitive, meaning it will only remove the suffix if the case matches exactly.
Discussions

How to remove a string suffix in Python? - TestMu AI Community
How do I remove part of a string in Python from the end (remove a suffix of the string)? More on community.testmu.ai
๐ŸŒ community.testmu.ai
0
December 5, 2024
How do I remove remove stuff like (. , ' *) from a string?
You can use isalnum() to remove special characters. The string method .isalnum() returns True or False if a string is all alphanumeric characters. With this we can write a comprehension that loops over the characters in the string and only keep the alphanumeric characters. I think the regex method is more efficient, especially for really big strings, but you have to know all the characters you want to remove, with isalnum you don't. On the flipside you don't have to add another import if you use isalnum. So it depends on the situation for which method you should choose (re vs isalnum). s = "Bob 'hit' a ball, the hit BALL flew far after it was hit." # Remove all special characters unless it is a space # Replace "hit" with "sample" new_s = "".join(char for char in s if char.isalnum() or char == " ").replace("hit", "sample") print(new_s) >>>Bob sample a ball the sample BALL flew far after it was sample More on reddit.com
๐ŸŒ r/learnpython
14
1
August 6, 2021
help me understand why removeprefix() isn't working in this case
First, you should format your code for Reddit by adding four spaces before each line of code. for x in range(len(random_data["data"])): for n, i in random_data["data"][x].items(): print(n.removeprefix("name"), i.removeprefix("test")) Second, you can just iterate over the list directly: for d in random_data["data"]: # Forgive the name, I know nothing about the context for n, i in d.items(): print(n.removeprefix("name"), i.removeprefix("test")) Third, there's only so much we can do without a proper example. Can you show us what these dictionaries look like? More on reddit.com
๐ŸŒ r/learnpython
3
1
December 22, 2022
Pathlib is cool

Its awesome, I love the read/write_text/bytes functions so convenient!

More on reddit.com
๐ŸŒ r/Python
196
483
July 28, 2022
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ python-remove-the-prefix-and-suffix-from-a-string
Python: Remove the Prefix and Suffix From a String
March 7, 2023 - This gives the following output ... 5 After: xyxyxyxy | yzyzyzyzyz 4 ยท To remove the suffix, we just adjust the order of parameters in the slicing notation to iterate from the end of the string:...
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ strings โ€บ .removesuffix()
Python | Strings | .removesuffix() | Codecademy
August 28, 2025 - The str.removesuffix() method returns a new string with the specified suffix removed, if present. If the string does not end with the given suffix, the original string is returned unchanged.
๐ŸŒ
TestDriven.io
testdriven.io โ€บ tips โ€บ fe2e27db-b5ab-4f9f-b59b-472a7d9f6594
Tips and Tricks - Python - remove a suffix from a string | TestDriven.io
You can remove the suffix of a string with .removesuffix(). For example, to remove the file type from a filename:
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python strings โ€บ how to remove a prefix or suffix from a string in python
How to Remove a Prefix or Suffix from a String in Python โ€ข datagy
December 16, 2022 - How to use string slicing to remove a prefix or a suffix ... If you are using Python 3.9 or later, the best way to remove a suffix from a string in Python is to use the .removeprefix() method.
๐ŸŒ
Python
peps.python.org โ€บ pep-0616
PEP 616 โ€“ String methods to remove prefixes and suffixes | peps.python.org
March 19, 2020 - This is a proposal to add two new methods, removeprefix() and removesuffix(), to the APIs of Pythonโ€™s various string objects. These methods would remove a prefix or suffix (respectively) from a string, if present, and would be added to Unicode ...
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-remove-suffix-from-string-list
Python - Remove suffix from string list - GeeksforGeeks
January 14, 2025 - To remove a suffix from a list of strings, we identify and exclude elements that end with the specified suffix.
๐ŸŒ
Kieran Barker
barker.codes โ€บ blog โ€บ removing-prefixes-and-suffixes-in-python
Removing prefixes and suffixes in Python - Kieran Barker
January 16, 2023 - Not only is this more readable, ... โ‰ค 3.8, the easiest way to remove a prefix (or suffix) from a string is to check if the string starts with the prefix (or ends with the suffix) and slice it....
๐ŸŒ
Quora
quora.com โ€บ Whats-the-best-way-to-remove-a-suffix-of-a-string-in-Python
What's the best way to remove a suffix of a string in Python? - Quora
Answer (1 of 2): It depends on the suffix - If then suffix is always there, and is a fixed length - then simply use slicing : To remove the last n characters from a string : [code]the_string = the_string[:-n] [/code]If the suffix is variable length, but always has a particular set of character...
๐ŸŒ
All About AI-ML
indhumathychelliah.com โ€บ 2020 โ€บ 10 โ€บ 12 โ€บ new-string-methods-to-remove-prefixes-and-suffixes-in-python-3-9
New String Methods to Remove Prefixes and Suffixes in Python 3.9 โ€“ All About AI-ML
January 2, 2022 - removeprefix,removesuffix โ†’ only at most one copy of prefix/suffix is removed. lstrip,rstrip โ†’ the characters are repeatedly removed from the beginning/end of the string.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ removesuffix_method.htm
Python String removesuffix() Method
The Python string removesuffix() method is used to remove a specified suffix from the end of a string. If the string ends with the specified suffix, the method removes the suffix from the string and returns the modified string.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ python-program-to-delete-suffix-from-the-given-string
Python program to delete suffix from the given string
March 27, 2026 - Use string slicing with endswith() for precise suffix removal. The rstrip() method works well for character removal, while replace() is useful when combined with conditional checks. Choose the method that best fits your specific use case.
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ prefixes-and-suffixes
Checking for string prefixes and suffixes in Python - Python Morsels
August 12, 2025 - To remove a prefix or suffix, use the removeprefix or removesuffix methods. If you need to remove repeated characters from either end of a string, check out the various strip methods instead.
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-string-removesuffix
Python String removesuffix()
Since the original string "HelloWorld" does not end with the suffix value "Apple", removesuffix() returned a copy of the original string. In this tutorial of Python Examples, we learned about string removesuffix() method, and how to use this string removesuffix() method to remove the specified ...
๐ŸŒ
Medium
medium.com โ€บ @asierr โ€บ pythons-str-removeprefix-and-str-removesuffix-finally-a-cleaner-way-to-modify-strings-468ee7114eb5
Python's str.removeprefix() and str.removesuffix(): Finally, a ...
February 18, 2025 - In this article, weโ€™ll explore what these methods do, why they were introduced, and how they can simplify your Python string manipulations. ๐Ÿš€ ... Before Python 3.9, developers had to use manual slicing or regular expressions to remove prefixes and suffixes. text = "prefix_data" if text.startswith("prefix_"): text = text[len("prefix_"):] print(text) # "data" text = "data_suffix" if text.endswith("_suffix"): text = text[:-len("_suffix")] print(text) # "data"
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ python-remove-suffix
Python `remove_suffix` Method: A Comprehensive Guide - CodeRivers
April 16, 2025 - The remove_suffix method is a built - in string method in Python 3.9 and later. It takes a single argument, which is the suffix string that you want to remove from the end of the original string. If the original string ends with the specified suffix, the method returns the string with the suffix ...