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
🌐
freeCodeCamp
freecodecamp.org › news › how-to-substring-a-string-in-python
How to Substring a String in Python
January 3, 2020 - The character at this index is not included in the substring. If end is not included, or if the specified value exceeds the string length, it is assumed to be equal to the length of the string by default. step: Every "step" character after the current character to be included. The default value is 1. If step is not included, it is assumed to be equal to 1. string[start:end]: Get all characters from start to end - 1
Discussions

how to slice a string from the end
>>> foo = "file.txt" >>> foo[:] 'file.txt' >>> foo[1:] 'ile.txt' >>> foo[-4] '.' >>> foo[-4:] '.txt' More on reddit.com
🌐 r/learnprogramming
28
2
February 19, 2023
python - Slice to the end of a string without using len() - Stack Overflow
With string indices, is there a way to slice to end of string without using len()? Negative indices start from the end, but [-1] omits the final character. word = "Help" word[1:-1] # But I More on stackoverflow.com
🌐 stackoverflow.com
How to extract last three characters of a string?
echo "D:\\nim\\nim.zip"[^4..^1] More on reddit.com
🌐 r/nim
9
11
December 14, 2022
how to get what index position the last character of a string is
this could have been solved with 5 minutes of experimentation if you're going to be a programmer you have to start experimenting with code, just test and try stuff out More on reddit.com
🌐 r/learnpython
13
3
April 2, 2022
🌐
W3Schools
w3schools.com › python › ref_string_endswith.asp
Python String endswith() Method
Python Examples Python Compiler ... Q&A Python Bootcamp Python Training ... The endswith() method returns True if the string ends with the specified value, otherwise False....
🌐
Reddit
reddit.com › r/learnprogramming › how to slice a string from the end
r/learnprogramming on Reddit: how to slice a string from the end
February 19, 2023 -

hi, i'm playing with string slice and i don't understand some things. i know that the [-1] is the last charachter, [-2] the penultimate etc. so, i have a string like "file.txt" and i want to exctract only the ".txt" part of it. i tried with string[-1:-4] but i get an empty string; then i tried with string[:len(string)-5:-1] but i get "txt.". i don't know what i'm doing wrong, can someone explain it to me? thank you

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-remove-the-given-substring-from-end-of-string
Python | Remove the given substring from end of string - GeeksforGeeks
July 11, 2025 - In this method, we are using string slicing to remove the substring from the end. ... text = 'GeeksforGeeksWorld' sub = "World" # find len of suffix le = len(sub) # slice out from string text = text[:-le] print(text) ... In this method, we are using the Python loop and append method to remove the substring from the end.
🌐
Sentry
sentry.io › sentry answers › python › extract a substring from a string in python
Extract a substring from a string in Python
2 weeks ago - Extract substrings from Python strings using slice notation with [start:end] indexes, or use re.search() with regex patterns for matching specific formats
🌐
Codecademy
codecademy.com › docs › python › substrings
Python | Substrings | Codecademy
June 18, 2025 - To specify only an end index, use [:n], where n is the ending position. This will return the first n characters. ... The string method .find() can also be used to find a subset. It returns the index of the first occurrence of the substring.
Find elsewhere
🌐
Flexiple
flexiple.com › python › last-character-string-python
Get the Last Character of a String in Python - Flexiple
April 2, 2024 - To get the last character, you can use the slice [-1:], which includes the last character up to the end of the string. This method is useful when you need a substring containing the last character.
🌐
Jeremy Morgan
jeremymorgan.com › python › how-to-get-the-last-character-of-a-string-python
How to Get the Last Character of a String in Python
December 11, 2023 - In this example, we use the rfind() method to find the last occurrence of the substring “o” in the string. The method returns the index of the last occurrence, which is 4 in this case.
🌐
TutorialsPoint
tutorialspoint.com › how-do-i-remove-a-substring-from-the-end-of-a-string-in-python
Remove the given Substring from the End of a String using Python
December 7, 2022 - def remove_substring(string, substring): index = string.rfind(substring) if index != -1 and index + len(substring) == len(string): return string[:index] else: return string # Example whole_string = "Hello Everyone, I am John, the Sailor!" last_substring = ", the Sailor!" final_string = remove_substring(whole_string, last_substring) print(final_string) ... # Example using removesuffix() text = "Hello Everyone, I am John, the Sailor!" suffix = ", the Sailor!" result = text.removesuffix(suffix) print(result) ... Use removesuffix() if you're using Python 3.9+, otherwise endswith() provides the clearest approach.
🌐
W3Schools
w3schools.com › jsref › jsref_substring.asp
W3Schools.com
The substring() method extracts characters from start to end (exclusive).
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substring
String.prototype.substring() - JavaScript | MDN
The substring() method of String values returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string
Python String - GeeksforGeeks
Python · s = "Hello World" print(s.upper()) print(s.lower()) Output · HELLO WORLD hello world · 3. strip() and replace(): strip() removes leading and trailing whitespace from the string and replace() replaces all occurrences of a specified substring with another.
Published   June 11, 2026
🌐
W3Schools
w3schools.com › python › python_howto_reverse_string.asp
How to reverse a String in Python
There is no built-in function to reverse a String in Python. The fastest (and easiest?) way is to use a slice that steps backwards, -1. ... Create a slice that starts at the end of the string, and moves backwards.
🌐
W3Schools
w3schools.com › python › ref_string_rfind.asp
Python String rfind() Method
HTML Examples CSS Examples JavaScript Examples How To Examples SQL Examples Python Examples W3.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples jQuery Examples · HTML Certificate CSS Certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate jQuery Certificate Java Certificate C++ Certificate C# Certificate XML Certificate
🌐
Medium
medium.com › @ryan_forrester_ › remove-the-last-character-from-a-string-in-python-a16000d6c102
Remove the Last Character from a String in Python | by ryan | Medium
January 7, 2025 - The fastest and most readable way ... string that includes everything except the last character. The `-1` index tells Python to stop one character before the end....
🌐
PHP
php.net › manual › en › function.substr.php
PHP: substr - Manual
If you want to have a string BETWEEN two strings, just use this function: <?php function get_between($input, $start, $end) { $substr = substr($input, strlen($start)+strpos($input, $start), (strlen($input) - strpos($input, $end))*(-1)); return ...
🌐
Reddit
reddit.com › r/nim › how to extract last three characters of a string?
r/nim on Reddit: How to extract last three characters of a string?
December 14, 2022 -

I can't for the life of me find any documentation on how to do this so I apologize.

In python its

string = "D:\\nim\\nim.zip"
print(string[-4:])

This outputs .zip. I can see in Nim how to slice a string from the start (0 .. ^4) but not backwards. Is this possible without writing a huge function?

🌐
C# Corner
c-sharpcorner.com › article › how-to-get-the-last-n-characters-of-a-string-in-python
How To Get The Last N Characters Of A String In Python
July 27, 2023 - _str = "C# Corner" N_of_char = 2 for i in range(0,N_of_char): print(_str[len(_str)-(i+1)],end="") You can use the above example to print any number of last characters, you just need to change the "N_of_char" value to your desired value. I wanted to print the last 2 characters, so I have set that value to 2. In this article, we discussed 3 ways of printing the last N characters of a String in Python.