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:
xxxxxxxxxx
1
text = "ABC"
2
text = text.replace("B", "")
3
print("after: ", text) # AC
In this example, we use replace()
method to remove substring from the string.
xxxxxxxxxx
1
= "ABC"
2
3
print("String before:", )
4
5
# remove last 3 characters from a string
6
= . ("B", "")
7
8
print("String after: ", )
Output:
xxxxxxxxxx
1
String before: ABC
2
String after: AC
Note:
You can also use
removeprefix()
andremovesuffix()
method to remove substring from the beginning or the end of the string.