How do you remove all the alphabetic characters from a 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.
Introduction
Removing alphabetic characters from a string sounds simple until you have to define what "alphabetic" means. If you only care about ASCII letters, a basic regular expression is enough. If you need to handle accented characters or non-Latin scripts, you need a Unicode-aware approach.
Start with the Exact Requirement
There are at least two common interpretations:
- remove only
A-Zanda-z - remove every character that is classified as a letter in Unicode
Those are not the same. For example, é, ß, and Ж are letters, but they are not matched by the ASCII-only pattern [A-Za-z].
If the input is strictly English-like text, the ASCII pattern is fine:
Output:
The spaces and punctuation remain because only letters are removed.
Python Solutions
In Python, a regular expression is usually the shortest answer for ASCII letters.
If you want a solution without regular expressions, filter character by character:
The isalpha() approach is often better when the requirement is "remove letters in general," because it respects Unicode letter categories.
The tradeoff is semantic clarity:
- '
re.sub(r"[A-Za-z]", "", text)means ASCII letters only' - '
ch.isalpha()means Unicode alphabetic characters'
JavaScript Solutions
For JavaScript, the same distinction exists. If you only need ASCII letters:
If you want broader Unicode support in modern JavaScript engines, use Unicode property escapes:
The \p{L} property means "any Unicode letter." The u flag is required so the engine interprets the property escape correctly.
Deciding What to Keep
In many real tasks, you are not just removing letters. You are preserving a smaller allowed set, such as digits and decimal separators.
For example, if the real goal is "keep digits only," write that requirement directly:
That is usually better than saying "remove alphabetic characters" because it also removes spaces and punctuation in one pass.
Likewise, in JavaScript:
This shift in thinking matters because transformation bugs often come from solving the wrong problem too literally.
A Note on Performance
For typical application strings, regex versus manual iteration is not worth debating. Both are fast enough. Choose the version that expresses the requirement clearly.
Where performance does matter:
- very large text streams
- millions of repeated transformations
- Unicode-heavy input with complicated rules
In those cases, benchmark the actual workload instead of assuming the regex is always faster. The biggest gain usually comes from choosing the right rule set, not micro-optimizing the loop.
Handling Unicode Correctly
Unicode changes the question from "alphabetic" to "which scripts count as letters for this application?" For example, isalpha() in Python will treat many non-ASCII symbols as letters. That is correct for internationalized text, but wrong if you only wanted to strip English letters from an identifier.
A practical rule is:
- use
[A-Za-z]when the domain is explicitly ASCII - use
isalpha()or\p{L}when the domain is human language text
Put that decision into tests so future changes do not silently broaden or narrow the behavior.
Common Pitfalls
- Using
[A-Za-z]when the input may contain non-ASCII letters. That leaves accented and non-Latin letters untouched. - Using
isalpha()when the requirement was actually "English letters only." Unicode-aware behavior can remove more than expected. - Removing letters when the real goal is to keep digits or some other whitelist. Express the target output directly when possible.
- Forgetting regex flags in JavaScript. Unicode property escapes need the
uflag, and global replacement usually needsg. - Ignoring spaces and punctuation. Removing letters does not automatically normalize the rest of the string.
Summary
- Define whether "alphabetic" means ASCII letters or Unicode letters.
- In Python, use
[A-Za-z]for ASCII orisalpha()for broader letter detection. - In JavaScript, use
/[A-Za-z]/gfor ASCII or/\p{L}/gufor Unicode-aware removal. - If the true goal is to keep digits or another whitelist, write that rule directly.
- Add tests for accented characters and mixed-content strings so the behavior stays intentional.
Related reading
- How do you write a program to find if certain words are similar?
- How does Apple find dates, times and addresses in emails?
- How does Beam Search operate on the output of The Transformer?
- How does CountVectorizer deal with new words in test data?
- How does Fine-tuning Word Embeddings work?
- How does Keras 1d convolution layer work with word embeddings - text classification problem? Filters, kernel size, and all hyperparameter
- How does lucene index documents?
- How does mask_zero in Keras Embedding layer work?
.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.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.