Example:
Input: Output:
dustbin bin
if 'dust' in string:
new = string.split('dust')
listToStr = ''.join(map(str, new))
print(listToStr)
The above code works fine.
But if the input changes like this.
Input: Sample Output:
dustduuuustdustbin bin
The above code doesn’t work. Is there a solution to this?
>Solution :
Use a regular expression.
import re
result = re.sub(r'[dust]', '', string)
The regexp [dust] matches any of those characters, and all the matches are replaced with an empty string.
If you want to remove only the whole word dust, with possible repetitions of the letters, the regexp would be r'd+u+s+t+'.
If only u can be repeated, use r'du+st'.