Remove Special Characters from String in Python
Last Updated :
12 Apr, 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.
Using re.sub()
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. This method is highly efficient, making it ideal for cleaning complex strings.
Python
import re
s = "Data!@Science#Rocks123"
res = re.sub(r'[^a-zA-Z0-9]', '', s)
print(res)
OutputDataScienceRocks123
Explanation: re.sub() function replaces all non-alphanumeric characters matched by the pattern [^a-zA-Z0-9] with an empty string, leaving only letters and digits in the result.
Using str.isalnum()
str.isalnum() method checks if a character is alphanumeric (letters or numbers). Combined with list comprehension, we can build a new string by including only valid characters. This method is both Pythonic and easy to understand, with good performance on medium-sized strings.
Python
s = "Geeks,forGeeks! 123."
res = ''.join([char for char in s if char.isalnum()])
print(res)
Explanation: list comprehension iterate through each character in the string s and includes only those that are alphanumeric using char.isalnum(). The join() function then combines these characters into a new string.
Using translate()
translate() method removes or replaces specific characters in a string based on a translation table. Using str.maketrans('', '', string.punctuation), we can quickly remove all punctuation characters. This method is very fast but only works well when the characters to be removed are predefined.
Python
import string
s = "Data!@Science#Rocks123"
res = s.translate(str.maketrans('', '', string.punctuation))
print(res)
OutputDataScienceRocks123
Explanation: string.punctuation provides a list of all standard punctuation characters and str.maketrans('', '', string.punctuation) creates a translation table that maps each punctuation character to None. When passed to translate(), this table removes all punctuation, leaving only letters, digits and spaces.
Using for loop
This method manually iterates through each character, checking if it's alphanumeric using char.isalnum(). It’s beginner-friendly and easy to understand, but inefficient due to repeated string concatenation.
Python
s = "Geeks,forGeeks! 123."
res = ''
for char in s:
if char.isalnum():
res += char
print(res)
Explanation: for loop iterates through each character in the string s and checks if it's alphanumeric using char.isalnum(). If it is, the character is added to the result string res.
Similar Reads
Python | Remove trailing/leading special characters from strings list Sometimes, while working with String lists, we can have a problem in which we need to perform the deletion of extra characters that can be termed as unwanted and occur at end of each string. Let's discuss a way in which this task can be performed. Method 1: Using map() + str.strip() A combination of
5 min read
Remove spaces from a string in Python Removing spaces from a string is a common task in Python that can be solved in multiple ways. For example, if we have a string like " g f g ", we might want the output to be "gfg" by removing all the spaces. Let's look at different methods to do so:Using replace() methodTo remove all spaces from a s
2 min read
Python | Remove given character from Strings list Sometimes, while working with Python list, we can have a problem in which we need to remove a particular character from each string from list. This kind of application can come in many domains. Let's discuss certain ways to solve this problem. Method #1 : Using replace() + enumerate() + loop This is
8 min read
Remove Multiple Characters from a String in Python Removing multiple characters from a string in Python can be achieved using various methods, such as str.replace(), regular expressions, or list comprehensions. Each method serves a specific use case, and the choice depends on your requirements. Letâs explore the different ways to achieve this in det
2 min read
Removing newline character from string in Python When working with text data, newline characters (\n) are often encountered especially when reading from files or handling multi-line strings. These characters can interfere with data processing and formatting. In this article, we will explore different methods to remove newline characters from strin
2 min read
Python - Remove Rear K characters from String List Sometimes, we come across an issue in which we require to delete the last characters from each string, that we might have added by mistake and we need to extend this to the whole list. This type of utility is common in web development. Having shorthands to perform this particular job is always a plu
5 min read