A substring is a chain of sequential characters inside a bigger string. Removing a substring from a fixed of strings deletes this substring from every detail within the set.Use str.replace() inside a for-loop to get rid of a substring from all strings in a fixed set of strings.
Use str.replace(old, new) with new as "" to remove substring old from str.
`_string = "programming"
new_string = a_string.replace("ing", "")
print(new_string)
Output
programm
`
Use str.replace() in a for loop to remove a substring from all elements in the set. Add each modified string to a new set.
string_set = {"learning", "enjoying", "programming"}
new_set = set()
for a_string in string_set:
new_string = a_string.replace("ing", "")
new_set.add(new_string)
print(new_set)
Output
{'enjoy', 'learn', 'programm'}
Use a set comprehension for a more compact implementation. Read more
Top comments (0)