-2

I want to remove ALL symbols from my string efficiently.

x = hello!!
r = dict.fromkeys(map(ord, '\n ' + string.punctuation))
x.translate(r)

I expected this to remove all symbols instead of only a full stop (.)

CAKEe
  • 105
  • 1

1 Answers1

1

What about using re.sub to remove all string.punctuation and ' \n':

x = re.sub('|'.join(map(re.escape, string.punctuation + ' \n')), '', x)

You could also use the following regex if you want to keep only alphabet and digit characters:

x = re.sub('[^a-zA-Z0-9]', '', x)
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55