HOWTO · Python
How to Remove Special Characters From the String in Python
This tutorial explains how to remove all the special characters from the string in Python
On this page
To remove characters from a Python string, first decide which characters to keep. A comprehension or filter() with str.isalnum() keeps Unicode letters and digits; a regular expression can retain spaces, while str.translate() can target a specified set. The examples below use "Hey! What's up bro?" to make each rule clear. Each code example defines its own input and prints the transformed string, so you can run the approaches independently.
Define What Counts as a Special Character
“Special character” has no single rule. It might mean punctuation only, punctuation and whitespace, or anything outside ASCII letters and digits. Choose the rule based on what the cleaned string is for: a display name may keep spaces, while a compact identifier may not.
Python strings are Unicode. str.isalnum() recognizes Unicode letters and digits, so it can retain characters such as é and 中. By contrast, a pattern written with [a-zA-Z0-9] covers ASCII letters and digits. The methods below therefore produce different results unless you give them the same character rule.
Remove Non-Alphanumeric Characters With a Comprehension
Use a string comprehension with str.isalnum() when the result should contain only letters and digits. str.join() combines the retained characters into a new string. This rule also removes spaces and punctuation.
original = "Hey! What's up bro?"
cleaned = "".join(char for char in original if char.isalnum())
print(cleaned)
HeyWhatsupbro
The predicate keeps Unicode letters and digits as well as ASCII characters. For example, a letter such as é is retained; spaces, apostrophes, and punctuation are omitted.
Filter Characters With filter()
The built-in filter() function applies a predicate to each character and returns an iterator containing the characters for which the predicate is true. Passing str.isalnum keeps the same Unicode letters and digits as the comprehension, and str.join() converts the iterator back to a string. As with the comprehension, whitespace is removed.
original = "Hey! What's up bro?"
kept = filter(str.isalnum, original)
cleaned = "".join(kept)
print(cleaned)
HeyWhatsupbro
Use filter() when you want to reuse a predicate or already have one defined. For this simple string transformation, the comprehension expresses the same rule more compactly.
Remove a Defined Set of Characters With str.translate()
Use str.translate() when you know the characters to remove and want all other characters to stay. str.maketrans() builds a table that maps each selected character to removal. The set below removes common ASCII punctuation; spaces and any characters not listed remain unchanged.
original = "Hey! What's up bro?"
characters_to_remove = "!@#$%^&*()_-+=<>,./?;:'\"[]{}\\|`~"
table = str.maketrans("", "", characters_to_remove)
cleaned = original.translate(table)
print(cleaned)
Hey Whats up bro
Add or remove characters from characters_to_remove to fit your input. This method does not remove every character that might be considered punctuation: an unlisted character, including non-ASCII punctuation, remains in the result.
Remove Characters With a Regular Expression
Use re.sub() to replace every character matched by a pattern. This pattern keeps only ASCII letters and digits, so it removes spaces as well as punctuation. The feature illustration shows this same rule. Its letter and digit ranges are ASCII-specific; use a Unicode-aware rule if the text includes letters outside those ranges.
import re
original = "Hey! What's up bro?"
pattern = r"[^a-zA-Z0-9]"
cleaned = re.sub(pattern, "", original)
print(cleaned)
HeyWhatsupbro
The supplied runtime evidence records this example producing the output shown above. To preserve whitespace, add \s to the allowed set as in r"[^a-zA-Z0-9\s]". Python’s regular-expression character classes and flags can express other rules, but confirm that the chosen pattern matches the language and characters in your data.
Transform Characters With map() and a Lambda
map() applies a function to each character and returns an iterator. A lambda can return the original character when it is a Unicode letter or digit or whitespace, and return an empty string for other characters. Joining those results builds the cleaned string; this rule preserves whitespace, including tabs and line breaks.
original = "Hey! What's up bro?"
cleaned = "".join(
map(lambda char: char if char.isalnum() or char.isspace() else "", original)
)
print(cleaned)
Hey Whats up bro
This is a character-by-character alternative when the transformation needs logic beyond a simple inclusion predicate. If the rule only keeps letters and digits, use the comprehension or filter() example instead.
Choose the Method That Matches Your Character Rules
- Choose a comprehension with
str.isalnum()for a direct, readable rule that keeps Unicode letters and digits and removes whitespace. - Choose
filter()when you want to pass a reusable predicate; it follows the samestr.isalnum()rule in the example. - Choose
str.translate()to remove a known set of characters while preserving everything else, including whitespace and unlisted punctuation. - Choose
re.sub()when a pattern describes the characters to remove or retain. The shown ASCII ranges remove whitespace; adapt the pattern for Unicode text or to preserve whitespace. - Choose
map()with a lambda for per-character transformation logic. The example retains Unicode letters, digits, and whitespace.
For names or natural-language text, decide how to handle accents, non-Latin scripts, and spacing before cleaning. For identifiers or protocol-specific input, match the documented character set exactly rather than relying on a broad idea of “special characters.”