-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 (.)

2
  • What dou mean by symbols? Do you want to keep only ascii-chars? Only letters and numbers? Commented Sep 10, 2019 at 9:51
  • Yeah, I would like to keep letters and numbers and remove all other characters such as punctuation Commented Sep 10, 2019 at 9:55

1 Answer 1

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) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.