0

I would like to delete some characters into a string (text with several words).

For this purpose I use the code below:

text2 = text.replace(',', '').replace('\n', '').replace('.', '').replace(':', '')

I have to add one replace method for each character to replace.

Is there any form to code in a smarter way? Something like this:

text2 = text.replace(',' '.' '\n' ':', '') # of course this is not working, just an example about how it could be.

Thank you

Claytor
  • 27
  • 7

1 Answers1

0

use re.sub(r'[,.:\n]', '', text)

TallChuck
  • 1,725
  • 11
  • 28