Open In App

Remove Special Characters from String in Python

Last Updated : 12 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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)

Output
DataScienceRocks123

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)

Output
GeeksforGeeks123

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) 

Output
DataScienceRocks123

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) 

Output
GeeksforGeeks123

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.



Next Article

Similar Reads