0

I want to remove specific characters from a string. I am using the following code:

words = words.replace("[", "").replace("_", "").replace("[","").replace("]", "")\
.replace("(", "").replace("/", "")

This code works fine but it is a bit messy. I am wondering if there is a better way to do this?

Epsi95
  • 8,832
  • 1
  • 16
  • 34

1 Answers1

1

Use re.sub with a character class containing the individual characters you want to remove:

words = re.sub(r'[\[\]_(/]', '', words)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360