Stripping everything but alphanumeric chars from a string in Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If you want to remove everything except letters and digits from a Python string, the best approach depends on what "alphanumeric" means in your context. For ASCII-only cleaning, a regular expression is concise. For Unicode-aware behavior, filtering characters with str.isalnum() is often the safer choice.
ASCII-Only Cleaning With re.sub
For a strict letters-and-digits rule using ASCII ranges:
The pattern [^A-Za-z0-9] means "any character that is not an ASCII letter or digit." Replacing those matches with an empty string removes punctuation, spaces, and symbols.
This is a good fit when you need predictable ASCII output, such as slug pre-processing or strict identifier cleanup.
Unicode-Aware Cleaning With isalnum
If you want to keep non-ASCII letters and digits as well, use str.isalnum():
This keeps characters that Python considers alphanumeric according to Unicode rules. That is often the more correct answer for user-facing text.
Keep Spaces While Removing Punctuation
Sometimes the real requirement is "remove symbols, but keep words separated." In that case:
That distinction matters because removing spaces entirely can merge words in a way that breaks search terms or human readability.
Normalize Whitespace After Cleaning
If punctuation removal leaves messy spacing, normalize it:
This is useful in text-cleaning pipelines where punctuation becomes separators rather than simply disappearing.
Turn It Into A Reusable Function
Making the behavior explicit helps avoid confusion later:
That makes the rule visible at the call site instead of burying it in a one-off expression.
\W Is Not The Same As "Non-Alphanumeric"
You may see examples using \W:
This is shorter, but it behaves differently from a strict alphanumeric rule. In regex terms, \w often includes underscores, and under Unicode rules it may include more than just ASCII letters and digits. That may be fine, but it is a different contract.
If you need precise behavior, write the character policy explicitly.
Performance Notes
For typical application strings, both regex and generator-expression approaches are fast enough. The bigger concern is correctness:
- use regex when the allowed character set is simple and explicit
- use
isalnum()when Unicode-aware semantics matter
If you are processing huge volumes of text, benchmark with realistic input before optimizing the implementation.
Common Pitfalls
The biggest mistake is not deciding whether Unicode letters should be preserved. A-Za-z0-9 and str.isalnum() do not mean the same thing.
Another common issue is removing punctuation without thinking about whitespace. If commas, dashes, or slashes disappear entirely, words can collapse together in ways that hurt readability or later parsing.
Developers also sometimes use \W expecting strict ASCII alphanumeric behavior, then get surprised by underscores or Unicode handling.
Finally, make the rule match the use case. Cleaning a user-visible string, generating a slug, and building a database key may all require slightly different definitions of "allowed" characters.
Summary
- Use
re.sub(r"[^A-Za-z0-9]", "", text)for a strict ASCII-only rule. - Use
"".join(ch for ch in text if ch.isalnum())when Unicode-aware behavior matters. - Decide explicitly whether spaces should be preserved.
- Be careful with
\Wbecause it is not the same as a strict alphanumeric filter. - Optimize for correctness first, then benchmark if performance becomes important.
Related reading
- str.startswith with a list of strings to test for
- Subclass in type hinting
- Substitute multiple whitespace with single whitespace in Python
- Subtract one month from Datetime.Today
- subtuples for a tuple
- Sum a list of numbers in Python
- sum over a list of tensors in tensorflow
- super fails with error TypeError argument 1 must be type, not classobj when parent does not inherit from object
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.