Languages
[Edit]
EN

Python - remove substring from string between indexes

0 points
Created by:
Dirask Community
6810

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() and removesuffix() method to remove substring from the beginning or the end of the string.

Python - string (popular problems)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join