string manipulation
text processing
special characters
data cleaning
python strings

Remove all special characters, punctuation and spaces from string

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Removing special characters, punctuation, and spaces is a common preprocessing step for identifiers, search keys, and normalized comparisons. The right approach depends on whether you must keep letters only, letters and digits, or full Unicode alphabets. In Python, re, str.translate, and Unicode-aware filtering each solve a different version of the problem.

Regex Approach for Alphanumeric Output

If your target is ASCII letters and digits only, regex is concise and easy to audit.

python
1import re
2
3def keep_alnum_ascii(text: str) -> str:
4    return re.sub(r"[^A-Za-z0-9]", "", text)
5
6print(keep_alnum_ascii("Hello, World! 2026"))
7print(keep_alnum_ascii("A_B-C+D"))

This drops spaces and punctuation in one pass.

str.translate for High Throughput

When you process many strings, translate can be faster because it uses a translation table. It is especially useful when the remove set is clearly defined.

python
1import string
2
3REMOVE = string.punctuation + string.whitespace
4TABLE = str.maketrans("", "", REMOVE)
5
6def clean_translate(text: str) -> str:
7    return text.translate(TABLE)
8
9print(clean_translate("Room #42, floor 7
10"))
11print(clean_translate("one	two	three"))

translate avoids regex engine overhead and is easy to benchmark.

Unicode-aware Filtering

For international text, ASCII regex can remove valid letters. A safer approach is str.isalnum, which respects Unicode categories.

python
1def keep_alnum_unicode(text: str) -> str:
2    return "".join(ch for ch in text if ch.isalnum())
3
4print(keep_alnum_unicode("cafe-42"))
5print(keep_alnum_unicode("ber cool! 99"))

If you also need case normalization, chain .casefold() before filtering.

python
1def normalized_key(text: str) -> str:
2    return "".join(ch for ch in text.casefold() if ch.isalnum())
3
4print(normalized_key("User-Name 2026"))

Choosing the Right Rule Set

Before implementing, define exact acceptance criteria. Many bugs come from vague requests like "remove special characters" without a formal character policy. Document whether underscores are allowed, whether accents should be preserved, and whether digits are required.

Then write tests that include punctuation, tabs, non-ASCII letters, emoji, and empty strings. Explicit tests prevent accidental behavior changes when refactoring parser logic.

Benchmark and Policy Alignment

Sanitization code often sits on hot paths such as search indexing or real-time request normalization. Benchmark candidate methods using representative data before committing to one implementation. Performance can differ significantly between regex and translation-table approaches.

python
1import timeit
2sample = "User-Profile #2026	Toronto!"
3
4print("regex:", timeit.timeit(lambda: keep_alnum_ascii(sample), number=200000))
5print("translate:", timeit.timeit(lambda: clean_translate(sample), number=200000))
6print("unicode:", timeit.timeit(lambda: keep_alnum_unicode(sample), number=200000))

Beyond speed, align implementation with policy owners. Security, analytics, and product teams may each require different normalization behavior. Keep a documented rule set and expose sanitizer versions in logs when behavior changes. That traceability reduces confusion during incident response.

If sanitized output is used as a key, keep the original value alongside the normalized form for traceability. This helps debugging collisions where different raw inputs collapse into the same cleaned string. Traceability is essential for audits, moderation workflows, and support investigations.

Before deploying sanitization changes, run a backfill on historical samples and compare key metrics such as match rates and collision counts. This protects downstream systems from sudden behavior shifts caused by seemingly small character-policy updates.

Document these rules in project docs so future maintainers apply the same behavior consistently.

Common Pitfalls

  • Using ASCII-only regex when data includes non-English characters.
  • Removing spaces without first deciding whether word boundaries should be preserved.
  • Treating sanitization for display and sanitization for identifiers as the same requirement.
  • Ignoring normalization rules such as casefolding and Unicode composition.
  • Applying aggressive cleaning to user-visible text and losing meaningful information.

Summary

  • Regex is concise for ASCII alphanumeric filtering.
  • str.translate is efficient for high-volume known character removal.
  • Unicode-aware filtering with isalnum is safer for multilingual data.
  • Define character policy before implementation to avoid ambiguous behavior.
  • Protect sanitization behavior with tests that cover edge cases.

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.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.