For instance I have a string that needs a certain keyword removed from a string so lets say may key word iswhatthemomooofun
and I want to delete the word moo
from it, I have tried using the remove function but it only removes "moo" one time, but now my string is whatthemoofun
and I can seem to remove it is there anyway I can do that?
CodePudding user response:
You can use the replace
in built function
def str_manipulate(word, key):
while key in word:
word = word.replace(key, '')
return word
CodePudding user response:
Have you tried using a while loop? You could loop through it until it doesn't find your keyword anymore then break out.
Edit
Cause answer could be improved by an example, take a look at the general approach it deals with:
Example
s = 'whatthemomooofun'
while 'moo' in s:
s= s.replace('moo','')
print(s)
Output
whatthefun
CodePudding user response:
original_string = "!(Hell@o)"
characters_to_remove = "!()@"
new_string = original_string
for character in characters_to_remove:
new_string = new_string.replace(character, "")
print(new_string)
OUTPUT
Hello