How to Remove All Occurrences of a Substring in a String

82 views

How to Remove All Occurrences of a Substring in a String

How to Remove All Occurrences of a Substring in a String

I have a string and I need to remove all instances of a specific substring. How can I do this?

solveurit24@gmail.com Answered question February 16, 2025
0

I have a string and I need to remove all instances of a specific substring. How can I do this?

Answer:
You can use the replace() method to replace all occurrences of a substring with an empty string.

Code Example:

original_string = "Hello world, world of programming."
substring = "world"
new_string = original_string.replace(substring, "")
print(new_string)


This code removes every occurrence of the substring “world” from the original string, resulting in “Hello , of programming.”.

solveurit24@gmail.com Changed status to publish February 16, 2025
0
You are viewing 1 out of 1 answers, click here to view all answers.