This can be done without regex:

>>> string = "Special $#! characters   spaces 888323"
>>> ''.join(e for e in string if e.isalnum())
'Specialcharactersspaces888323'

You can use str.isalnum:

S.isalnum() -> bool

Return True if all characters in S are alphanumeric
and there is at least one character in S, False otherwise.

If you insist on using regex, other solutions will do fine. However note that if it can be done without using a regular expression, that's the best way to go about it.

Answer from user225312 on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-removing-unwanted-characters-from-string
Remove Special Characters from String in Python - GeeksforGeeks
July 11, 2025 - ... Explanation: list comprehension ... these characters into a new string. translate() method removes or replaces specific characters in a string based on a translation table....
Discussions

How do I remove remove stuff like (. , ' *) from a string?
You can use isalnum() to remove special characters. The string method .isalnum() returns True or False if a string is all alphanumeric characters. With this we can write a comprehension that loops over the characters in the string and only keep the alphanumeric characters. I think the regex method is more efficient, especially for really big strings, but you have to know all the characters you want to remove, with isalnum you don't. On the flipside you don't have to add another import if you use isalnum. So it depends on the situation for which method you should choose (re vs isalnum). s = "Bob 'hit' a ball, the hit BALL flew far after it was hit." # Remove all special characters unless it is a space # Replace "hit" with "sample" new_s = "".join(char for char in s if char.isalnum() or char == " ").replace("hit", "sample") print(new_s) >>>Bob sample a ball the sample BALL flew far after it was sample More on reddit.com
🌐 r/learnpython
14
1
August 6, 2021
How to remove all special characters from an input, and accents?
try importing punctuations from string module which will contain most of the specials, then you can split your string in a list compression or for loop over all the chars and choose all the required chars with desired condition in a list and then join them for example: from string import punctuations a = "hello!" l1 = [] for x in a: if x not in punctuations: list1.append(x) and then you can use join method to make a str More on reddit.com
🌐 r/learnpython
7
0
March 18, 2023
Find and replace all special characters in all json keys
I agree with the other posts, use a parser if possible. But if you are somehow limited to just RegEx and your json is as simple as the example you posted you can do this. Building on u/Kompaan86 ‘s approach, and matching only before the : [^A-Za-z0-9\n"](?=[^:\n]*:) Regex101 demo This can break in 100 ways, so please use a parser if at all possible. More on reddit.com
🌐 r/regex
6
3
May 5, 2022
replace or remove special characters in variable..

Use the regex_replace jinja filter.

If you want to remove everything that's not A-Z, a-z, or 0-9, try something like:

{{ account_name | regex_replace("[^A-Za-z0-9]", "") }}
More on reddit.com
🌐 r/ansible
5
6
July 7, 2021
🌐
Medium
medium.com › @ryan_forrester_ › remove-special-characters-from-strings-in-python-complete-guide-53651c8163d9
Remove Special Characters from Strings in Python: Complete Guide | by ryan | Medium
January 7, 2025 - The `strip()` method is perfect for cleaning up the beginning and end of strings. When you need more control over character removal, regular expressions are your friend. Here’s a practical example: import re def clean_text(text): # Removes ...
🌐
Reddit
reddit.com › r/learnpython › how do i remove remove stuff like (. , ' *) from a string?
r/learnpython on Reddit: How do I remove remove stuff like (. , ' *) from a string?
August 6, 2021 -

I have a string:

"Bob 'hit' a ball, the hit BALL flew far after it was hit."

And I want to replace the word 'hit' by 'sample'.

How do I do that when the word gets immediately followed/preceded by stuff like (. , ' *)

Edit: I know using replace() will work but I don't know what special characters there will be in the string. Some strings had characters like ( - [ / + .

🌐
Scaler
scaler.com › home › topics › remove special characters from string python
Remove Special Characters From String Python - Scaler Topics
January 6, 2024 - The string.isalnum() method returns True if all the characters in the string are alphabets or numbers and returns False if it finds any special character in the string. We can use this property to remove all special characters from a string in python.
🌐
Esri Community
community.esri.com › t5 › python-questions › remove-letters-and-special-characters-from-an › td-p › 103183
Remove letters and special characters from an alpha numeric string using python
February 28, 2019 - I'm too much of a dolt in Python to see why. Here's what I'm getting: "syntaxerror: encoding declaration in unicode string (<expression>, line 0)" (see below) At first I thought it might be some characters that were causing the issue (hyphens, slashes, spaces), but eliminating them didn't help.
Find elsewhere
🌐
Jinja
jinja.palletsprojects.com › en › stable › templates
Template Designer Documentation — Jinja Documentation (3.1.x)
If a string that you marked safe is passed through other Python code that doesn’t understand that mark, it may get lost. Be aware of when your data is marked safe and how it is processed before arriving at the template. If a value has been escaped but is not marked safe, auto-escaping will still take place and result in double-escaped characters...
🌐
Vultr
docs.vultr.com › python › examples › remove-punctuations-from-a-string
Python Program to Remove Punctuations From a String | Vultr Docs
December 27, 2024 - import string def remove_punctuation_comprehension(input_string): result = ''.join([char for char in input_string if char not in string.punctuation]) return result sample_text = "Great work, everyone!" cleaned_text = remove_punctuation_comprehension(sample_text) print(cleaned_text) Explain Code · The list comprehension checks every character in input_string and includes it in the result list if it is not a punctuation mark.
🌐
Python documentation
docs.python.org › 3 › tutorial › introduction.html
3. An Informal Introduction to Python — Python 3.14.6 documentation
In the Python shell, the string definition and output string can look different. The print() function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters:
🌐
DevGenius
blog.devgenius.io › text-preprocessing-turning-messy-text-into-something-a-machine-can-actually-understand-c306ea2672d4
Text — Preprocessing : Turning Messy Text into Something a Machine Can Actually Understand | by Sujan Sharma | Apr, 2026 | Dev Genius
April 10, 2026 - Raw Text → Lowercasing → Noise Removal (HTML, URLs, special characters) → Tokenisation → Stop Word Removal → Stemming / Lemmatisation → (Optional) Spelling Correction, N-grams → Clean Tokens ✓ · Not all steps apply to every use case. A sentiment analysis model on tweets needs different preprocessing than a medical document retrieval system. The simplest step, but the one most people forget the exception for. Lowercasing converts all text to lowercase so that “Python”, “python”, and “PYTHON” are treated as the same token.
🌐
Netlify
ittutoria-removechar.netlify.app
Remove Special Characters From String Python
The regular expression will remove all special characters from the string. [aZA-Z0-9] will be the regular expression. is any character that is not in brackets. import re string = input('Enter any string: ') new_string = re.sub(r'[^a-zA-Z0-9]','',string) print('New string:', new_string) ...
🌐
datagy
datagy.io › home › python posts › python strings › python: remove special characters from a string
Python: Remove Special Characters from a String • datagy
December 17, 2022 - Python has a special string method, .isalnum(), which returns True if the string is an alpha-numeric character and returns False if it is not. We can use this, to loop over a string and append, to a new string, only alpha-numeric characters. ... # Remove Special Characters from a String Using .isalnum() text = 'datagy -- is.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-remove-character-from-string
How to Remove Characters from a String in Python | DigitalOcean
May 31, 2026 - Use str.replace() for a single character or substring, str.translate() or str.maketrans() to drop several characters in one pass, re.sub() for pattern-based removal, and slicing when you need to remove characters at the start, end, or a fixed index. This tutorial walks through each approach ...
🌐
Medium
medium.com › @vidvatek › how-to-remove-special-characters-from-string-in-python-13b04516421a
How to Remove Special Characters from String in Python | Medium
December 15, 2023 - This code demonstrates how to remove a specific character from a string using the join() method and list comprehensions in Python. Remove Special Characters from String using filter()
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations
May 25, 2026 - Source code: Lib/re/ This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings ( str) as well as 8-...
🌐
Semantic Versioning
semver.org
Semantic Versioning 2.0.0 | Semantic Versioning
And one with numbered capture groups instead (so cg1 = major, cg2 = minor, cg3 = patch, cg4 = prerelease and cg5 = buildmetadata) that is compatible with ECMA Script (JavaScript), PCRE (Perl Compatible Regular Expressions, i.e. Perl, PHP and R), Python and Go.
🌐
Wikipedia
en.wikipedia.org › wiki › ANSI_escape_code
ANSI escape code - Wikipedia
3 weeks ago - The ITU's T.416 Information technology ... characters instead: ESC[38:5:⟨n⟩m Select foreground color where n is a number from the table below ESC[48:5:⟨n⟩m Select background color · To calculate the RGB values of the colors in the table above, the following Python script can ...