You need to call replace
on z
and not on str
, since you want to replace characters located in the string variable z
removeSpecialChars = z.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
But this will not work, as replace looks for a substring, you will most likely need to use regular expression module re
with the sub
function:
import re
removeSpecialChars = re.sub("[!@#$%^&*()[]{};:,./<>?\|`~-=_+]", " ", z)
Don't forget the []
, which indicates that this is a set of characters to be replaced.