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 - Let's explore different methods to achieve this. 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
machine learning - remove special character in a List or String - Data Science Stack Exchange
Input_String is Text_Corpus of Jane Austen Book output Should be : ['to', 'be', 'or', 'not', 'to', 'be', 'that', 'is', 'the', 'question'] But getting this Output : ['to', 'be,', 'or', 'not', 'to'... More on datascience.stackexchange.com
🌐 datascience.stackexchange.com
October 20, 2018
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
Powershell - Remove all characters except...
You can use regex and a character class to tell -replace what you want to keep or remove. [a-zA-Z0-9-\\] Using a character class you can tell it to match any of the characters within [ ] literally [a-zA-Z] - is the range of characters in lower and upper case [0-9] - is the range of numbers from 0-9 [-] - is the hyphen character literally [\\] - is the backslash character which is a special character so needs to be escaped with another backslash. Putting them all together and using this would be: 'This-is-a\string:with(lo*ts&of£$stu-=ff' -replace '[a-zA-Z0-9-\\]' To tell it to replace everything NOT in your character class you prefix it with a carat '[^a-zA-Z0-9-\\]' More on reddit.com
🌐 r/PowerShell
10
4
March 12, 2019
🌐
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 - The Alpha characters can be any length and the numbers can be any length. Sometime their can even be an alpha character at the end of the string...its totally random I need to strip out the the alpha's and any special characters or spaces from the string to leave me with just the number.
🌐
Built In
builtin.com › software-engineering-perspectives › python-remove-character-from-string
How to Remove Characters From a String in Python | Built In
If we want to remove that specific character, we can replace that character with an empty string. The replace() method will replace all occurrences of the specific character mentioned.
🌐
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 - To remove special characters from a string in python, we can use the re.sub() method. The method has the following syntax, The regex_pattern is the regex pattern used to match characters in a string. The replace_char is the character we will replace with the characters matching the regex pattern.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-remove-all-characters-except-letters-and-numbers
Remove All Characters Except Letters and Numbers - Python - GeeksforGeeks
October 29, 2025 - If it is, we add it to a new string to create a cleaned version of the original. ... The loop checks each character.
Find elsewhere
🌐
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.
🌐
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.
🌐
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 - In this post, you learned how to remove special characters from a Python string. You learned how to do this with the .isalphanum() method, the regular expressions library re, and the filter() function.
🌐
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 ( - [ / + .

🌐
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
🌐
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 - Remove a Specific Character from ... join() method, you can split the string into a list of characters, filter out the character you want to remove, and then join the remaining characters back together....
🌐
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
🌐
Sololearn
sololearn.com › en › Discuss › 2625621 › how-to-remove-special-characters-and-numbers-from-string-
How to remove special characters and numbers from string !
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
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 ...
🌐
W3docs
w3docs.com › python
Remove all special characters, punctuation and spaces from string | W3Docs
<div class="alert alert-info flex ... ... The function remove_special_characters uses the re.sub() method, which performs a search-and-replace on the input string. The regular expression [^a-zA-Z0-9]+ matches one or more characters that are not letters or numbers, and replaces them with an empty string...
🌐
IONOS
ionos.com › digital guide › websites › web development › removing characters from strings in python
How to remove a character from a string in Python - IONOS
December 10, 2024 - With this function, you can search for regular ex­pres­sions in strings and replace them with other char­ac­ters. import re original_string = "Hello, World! @#$%^&*" modified_string = re.sub(r'[@#$%^&*]', '', original_string) print(original_string) # Output: Hello, World! @#$%^&* print(modified_string) # Output: Hello, World!Python · The pattern [@#$%^&*] is a regular ex­pres­sion that matches the special char­ac­ters @, #, $, %, ^, &,*. The re.sub() function searches for all matches of the pattern in the original string original_string and replaces them with an empty string ''. In the example above, we saved the result in the variable modified_string and output it.