Stripping everything but alphanumeric chars from a string in Python

I just timed some functions out of curiosity. In these tests I’m removing non-alphanumeric characters from the string string.printable (part of the built-in string module). The use of compiled ‘[\W_]+’ and pattern.sub(”, str) was found to be fastest. $ python -m timeit -s \ “import string” \ “”.join(ch for ch in string.printable if ch.isalnum())” 10000 … Read more