Python
String Manipulation
Alphanumeric
Data Cleaning
Programming

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.

Browse interview questions

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:

python
1import re
2
3text = "User-42: ready?"
4clean = re.sub(r"[^A-Za-z0-9]", "", text)
5
6print(clean)  # User42ready

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():

python
1text = "café №42"
2clean = "".join(ch for ch in text if ch.isalnum())
3
4print(clean)

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:

python
1text = "Hello, world! 2025"
2clean = "".join(ch for ch in text if ch.isalnum() or ch.isspace())
3
4print(clean)  # Hello world 2025

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:

python
1text = "Hello,   world!   2025"
2clean = "".join(ch if ch.isalnum() or ch.isspace() else " " for ch in text)
3clean = " ".join(clean.split())
4
5print(clean)  # Hello world 2025

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:

python
1import re
2
3
4def strip_non_alnum(text: str, keep_spaces: bool = False) -> str:
5    if keep_spaces:
6        cleaned = re.sub(r"[^A-Za-z0-9\s]", "", text)
7        return " ".join(cleaned.split())
8    return re.sub(r"[^A-Za-z0-9]", "", text)
9
10
11print(strip_non_alnum("Hi, user-42!"))
12print(strip_non_alnum("Hi, user-42!", keep_spaces=True))

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:

python
clean = re.sub(r"\W+", "", text)

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 \W because it is not the same as a strict alphanumeric filter.
  • Optimize for correctness first, then benchmark if performance becomes important.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.