Remove All Occurrences of Substring

Remove all occurrences of a substring.

Logic BuildingIntermediate
Logic Building
# Take string input
s = input("Enter a string: ")
substring = input("Enter substring to remove: ")

# Remove substring
result = s.replace(substring, "")
print(f"After removal: {result}")

Output

Enter a string: Hello World Hello
Enter substring to remove: Hello
After removal:  World 

Use replace() to remove substring.

Key Concepts:

  • Replace substring with ""
  • All occurrences removed
  • Returns new string