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 - When working with text data in Python, it's common to encounter strings containing unwanted special characters such as punctuation, symbols or other non-alphanumeric elements. For example, given the input "Data!@Science#Rocks123", the desired output is "DataScienceRocks123". 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.
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 part of string after certain character in python?

There are a few ways. Here are a few off the top of my head:

>>> s = "abcd//efgh"

>>> s.find("/")
4
>>> s[:s.find("/")]
'abcd'

>>> s.split("/")
['abcd', '', 'efgh']
>>> s.split("/", maxsplit=1)
['abcd', '/efgh']
>>> s.split("/", maxsplit=1)[0]
'abcd'

>>> import re
>>> re.sub("/.*$", "", s)
'abcd'

The last is overkill here and I wouldn't use it, but regexs are often appropriate for doing search & replace operations. Either of the first two would work pretty well. The first depends on the search string appearing though. Otherwise, s.find will return -1 and then s[:-1] will lop off the last character:

>>> s = "abcdef"
>>> s[:s.find("/")]
'abcde'
More on reddit.com
🌐 r/AskProgramming
4
5
July 2, 2017
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
Get rid of a weird character (\x16)?
I had to clean up a few thousand html files that had various illegal characters in them. Here is a portion of what I did: import re for line in lines: line = re.sub("[\x93]", '"', line) More on reddit.com
🌐 r/learnpython
8
4
August 5, 2014
🌐
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 - 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 all special characters except spaces and alphanumeric characters cleaned = re.sub(r'[^a-zA-Z0-9\s]', '', text) return cleaned # Real-world example: Cleaning a product description product_desc = "Latest iPhone 13 Pro (128GB) - $999.99 *Limited Time Offer!*" clean_desc = clean_text(product_desc) print(clean_desc) # Output: "Latest iPhone 13 Pro 128GB 999.99 Limited Time Offer"
🌐
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.
🌐
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.
🌐
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
LabEx provides a range of built-in functions and algorithms that can be used to perform advanced string manipulation, including the removal of special characters, normalization, and text extraction. By integrating LabEx into your Python workflow, you can access these advanced string cleaning capabilities and streamline your data preprocessing and cleaning processes. In this Python tutorial, you have learned various techniques to remove special characters from strings, including using built-in methods like str.replace() and re.sub(), as well as more advanced approaches like regular expressions and custom functions.
🌐
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 - You can use the str.isalnum() method to remove special characters from a string in Python. Here's an example. # Define a string with special characters text_with_special_chars = "Hello @World!
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › python › remove special characters from string python
How to Remove Special Characters From the String in Python | Delft Stack
February 2, 2024 - The resulting translation_table maps each special character to None, effectively indicating that they should be removed from the string. Step 3: Apply the Translation using the translate() Method · With the translation table in place, we apply it to the original string using the translate() method. This method applies the translation table and returns a new string with the specified character substitutions or removals. The map() function is a built-in Python function that applies a given function to each item of an iterable (e.g., a list, tuple, or string) and returns an iterator.
🌐
Kaggle
kaggle.com › general › 233576
Remove Special Characters from Text, EXCEPT @'s and #'s - Python | Kaggle
Hello Everyone, I have some really messy text that I'm trying analyze, example below. "@KYMFAFO She’s so pretty!! You’re 1 lucky guy #babe" I was able to...
🌐
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.
🌐
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 ( - [ / + .

🌐
CodeSignal
codesignal.com › learn › courses › advanced-data-cleaning-handling-text-data-1 › lessons › removing-special-characters-and-normalizing-text-using-python
Removing Special Characters and Normalizing Text Using ...
The sub() function in the re module is a versatile tool for replacing unwanted characters, symbols, or redundant spaces in text data. This function is crucial for text normalization, as it allows you to systematically remove or replace elements that do not contribute to the meaning of the text.
🌐
Code Beautify
codebeautify.org › blog › remove-special-characters-from-string-python
Remove Special Characters From String Python
February 22, 2024 - One of the most powerful tools for string manipulation is regular expressions. Python’s re module provides a convenient way to match and replace patterns in strings. The following code snippet demonstrates how to remove special characters using regular expressions:
🌐
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
🌐
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 › @blueberry92450 › three-ways-to-remove-special-characters-from-string-in-python-da1035cc93b8
Three ways to Remove Special Characters from String in Python Including Time Comparison | Medium
August 8, 2022 - If you are fully aware of the character type range of the content you are dealing with and you want to replace specific characters with other character => make your own customized replace function
🌐
PyTutorial
pytutorial.com › remove-special-characters-from-string-in-python
PyTutorial | Remove Special Characters from String in Python
February 23, 2026 - You create this table with str.maketrans(). It is highly efficient for large texts. # String with various symbols data = "Product™ Code: ABC-123 • Price: €99.99" print("Original:", data) # Create a translation table # First arg: characters to replace (empty string here means remove) # Second arg: characters to delete # Third arg: characters to ignore (we don't need this now) # To just delete, we map None to the characters we want gone. # We specify the special characters.