To remove a specific substring from a string in Python, you can use the `replace` method of the String class.
In this example,
str
with a value of "Hello World"
.replace
method of the string object str
to replace the substring "World"
with an empty string ""
.replace
method returns a new string with all occurrences of the specified substring replaced.str
to the console.str = "Hello World"
str = str.replace("World", "")
print("Modified string:", str)
Modified string: Hello
In this example,
text
with a value of "I like apple pie"
.replace
method of the string object text
to replace the substring "apple"
with an empty string ""
.replace
method creates and returns a new string with all occurrences of the specified substring replaced.text
to the console.text = "I like apple pie"
text = text.replace("apple", "")
print("Modified string:", text)
Modified string: I like pie
In this tutorial, we learned How to Remove specific Substring in Python language with well detailed examples.