0

While iterating through different strings I sometimes stumble upon characters that contain strings such as "ś" or even just a " ' " and I want to replace those every single character with a simple "_".

So the code I would want is one that meets the following requirements: if character is not a,b,c....Z or a number then character = "_".

I wonder what is the most elegant way to implement such a code.

Thanks in advance.

MathiasRa
  • 825
  • 2
  • 12
  • 24
  • 1
    Possible duplicate of [Replace non-ASCII characters with a single space](https://stackoverflow.com/questions/20078816/replace-non-ascii-characters-with-a-single-space) – snakecharmerb Jul 01 '18 at 08:51

1 Answers1

2
import re

s = re.sub('[^A-Za-z0-9]', '_', s)
zvone
  • 18,045
  • 3
  • 49
  • 77
jspcal
  • 50,847
  • 7
  • 72
  • 76