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 - re.sub() function from re module allows you to substitute parts of a string based on a regex pattern. By using a pattern like [^a-zA-Z0-9], we can match and remove all non-alphanumeric characters.
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
delete all special characters at start and end of string
If you just need to remove single quotes from start/end of string, you can use s.strip("'") If you want to remove all non-alphanumeric characters from start/end, try re.sub(r'\A[^a-z\d]+|[^a-z\d]+\Z', '', s, flags=re.I) More on reddit.com
๐ŸŒ r/learnpython
17
37
January 25, 2022
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
how to remove part of a file name recursively in python?
What you want to do is remove the prefix. Your prefix is 31 chars >>> len("The.Simpsons.The.Simpsons.clear") 31 >>> t="The.Simpsons.The.Simpsons.clearThe.SimpsonsSeason 1 EP7 The Call of the Simpsons.mkv" >>> t[31:] 'The.SimpsonsSeason 1 EP7 The Call of the Simpsons.mkv' A bit of advice: before you do something permanent like renaming files, print out new names just to make sure they look ok. More on reddit.com
๐ŸŒ r/learnpython
7
9
March 8, 2015
๐ŸŒ
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 ...
๐ŸŒ
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.
๐ŸŒ
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 ( - [ / + .

๐ŸŒ
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.
Find elsewhere
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-remove-special-characters-from-a-python-string-397739
How to remove special characters from a Python string | LabEx
In this example, we use the str.maketrans() function to create a translation table that maps all punctuation characters to an empty string, effectively removing them from the string. These built-in methods provide a simple and efficient way to remove special characters from Python strings, making them a valuable tool for data cleaning and preprocessing tasks.
๐ŸŒ
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) ...
๐ŸŒ
Python Guides
pythonguides.com โ€บ remove-special-characters-except-for-space-from-a-string-in-python
Remove Special Characters From a String in Python
September 5, 2025 - You can refer to the screenshot below to see the output. This method filters out unwanted symbols using a generator and join(), creating a clean string with only the desired characters. The findall() function from the re module returns all non-repeated matches of a pattern in a string as a list of strings in Python. This function can find all alphanumeric substrings and spaces in the string. ... import re def remove_special_characters(text): return ''.join(re.findall(r'[a-zA-Z0-9\s]', text)) text = "Route '66โ€”th' ultimate #American road trip!" cleaned_text = remove_special_characters(text) print("Cleaned Text:", cleaned_text)
๐ŸŒ
Quora
quora.com โ€บ How-do-I-remove-all-special-characters-in-a-string-in-Python
How to remove all special characters in a string in Python - Quora
Answer (1 of 5): There are numerous ways to accomplish this. To remove, say, all the aโ€™s from a string, one can use the replace() string method: [code]>>> s = 'A man, a plan, a canal: Panama' >>> s = s.replace('a', '') >>> s 'A mn, pln, cnl: Pnm' [/code]One could also โ€œexplodeโ€ the string ...
๐ŸŒ
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()
๐ŸŒ
Linux Hint
linuxhint.com โ€บ remove-special-characters-string-python-2
Remove Special Characters from String Python โ€“ Linux Hint
The โ€œisalnum()โ€ method deletes the unwanted characters from a string in Python. It returns โ€œTrueโ€ when all the existing characters in the input string are alphabets or numbers. On the other hand, it will return a โ€œFalseโ€ value if any special character is found in the input string.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-remove-special-chars-from-strings-452160
How to remove special chars from strings | LabEx
This tutorial explores various techniques to effectively eliminate unwanted characters from strings, providing developers with practical solutions to handle text manipulation challenges. Special characters are non-alphanumeric symbols that are not letters (A-Z, a-z) or numbers (0-9).
๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-remove-all-special-characters-from-a-string-in-python
How to Remove All Special Characters from a String in Python - Javatpoint
April 21, 2023 - How to Remove All Special Characters from a String in Python with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-remove-a-specific-character-from-a-string-in-python
How to Remove a Specific Character from a String in Python
December 7, 2022 - Then, specify the group of characters you want to remove (in this case, the ! and ? characters), along with the characters you want to replace them with. In this case, the replacement is an empty character: import re my_string = "Hi!? I!? love!? Python!?" my_new_string = re.sub('[!?]',"",my_string) print(my_new_string) # output # Hi I love Python
๐ŸŒ
Code Beautify
codebeautify.org โ€บ blog โ€บ remove-special-characters-from-string-python
Remove Special Characters From String Python
February 22, 2024 - Another approach involves leveraging the ASCII values of characters to filter out special characters. This method is particularly useful if you want to preserve alphanumeric characters and spaces. Hereโ€™s an example of how this can be done: List comprehensions offer a concise and readable way to process strings.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ How-to-remove-specific-characters-from-a-string-in-Python
How to remove specific characters from a string in Python?
March 19, 2025 - The simplest way to remove specific characters from a string is to use the str.replace() method. This method allows you to replace occurrences of a specified character with another character or an empty string (to remove it).
๐ŸŒ
StudyMite
studymite.com โ€บ python โ€บ remove-special-characters-from-a-string-in-python-using-regex
Remove Special Characters from a String in Python Using Regex | StudyMite
March 2, 2023 - Learn how to remove special characters from a string in Python using Regex with various methods like using sub(), translate(),replace(),etc