EN
Python - remove substring from string between indexes
0
points
In this article, we would like to show you how to remove substring from the string in Python.
Quick solution:
text = "ABC"
text = text.replace("B", "")
print("after: ", text) # AC
Practical example
In this example, we use replace()
method to remove substring from the string.
text = "ABC"
print("String before:", text)
# remove last 3 characters from a string
text = text.replace("B", "")
print("String after: ", text)
Output:
String before: ABC
String after: AC
Note:
You can also use
removeprefix()
andremovesuffix()
method to remove substring from the beginning or the end of the string.