How to remove all non-alphanumeric characters from string in Python

Playing with strings is an essential step to start with any programming language. It gives us an idea of dealing with various situations or conditions in competitive programming.

By programming definition, a string is a data type used to store a collection of characters, i.e., it can contain alphanumeric characters (i.e., A-Z, a-z, 0-9) and special symbols (!, @, %, _, etc.).

Strings are always enclosed in single/double-quotes. For example:

str = "Pass@123"
print(type(str))

s = 123
i = "123"

print(type(s))
print(type(i))

Output

<class 'str'>
<class 'int'>
<class 'str'>

And one of the most common questions asked in placement interviews is “How to remove all non-alphanumeric characters from string in Python?

Analyze the problem

Input and Output

To solve removing all non-alphanumeric characters from a string, we should first know what the input and output should be. According to the problem statement,

  • There would be only single input with data type as ‘String’ as it is mentioned in the question.
  • The output would also be single with ‘String’ as a data type, as we have to show them the String with only alphanumeric characters.

Conditions

  • The program/code should return a string containing only alphabets and numeric characters, i.e., A to Z, z, and 0-9.
  • It should not contain special symbols such as ‘@’, ‘!’, ‘?’, etc.
  • It should not contain space as well.

Libraries

There is no need to import libraries as the problem statement relates to string manipulation. Still, if needed, we can use the RE library, a Regular expression library that helps us check if the specified string matches according to the condition given.

Remove all non-alphanumeric characters from String in Python

To remove all non-alphanumeric characters from a string in Python, use the filter() method. The filter() is a built-in Python method that extracts items from an iterable-like string for which a function returns True. The isalnum() is a built-in Python method that returns True if all characters in the String are alphanumeric (either alphabets or numbers). If not, it returns False.

Syntax

filter(function, iterable)

Arguments

The filter() function accepts a function and iterable. A function is a function, and an iterable is String.

Example

inputString = "Is your email id datascience_123@gmail.com"
outputString = ''.join(filter(str.isalnum, inputString))
print(outputString)

Output

Isyouremailiddatascience123gmailcom

You can see that the input string had non-alphanumeric characters, but after using the filter() function with isalnum() and join() method, we removed all the special characters from the String.

The time complexity of the filter() method is o(1).

Removing special character from String using isalnum()

To remove special characters from a string in Python, use the isalnum() function. The isalnum() is a built-in Python function that returns True if all characters in the string are alphanumeric, and If not, it returns False.

Syntax

string.isalnum()

Arguments

The isalnum() function doesn’t take any parameters.

Example

inputString = "Is your email id datascience_123@gmail.com"
outputString = ''.join(i for i in inputString if i.isalnum())
print(outputString)

Output

Isyouremailiddatascience123gmailcom

You can see that the isalnum() function helps us identify special characters, and then we remove it and join the string.

The time complexity is  O(n), where n is the size of a string.

Remove special characters from a string using Regular Expression

A regular expression (or RE) in Python defines a set of strings that matches it. We can remove non-alphanumeric characters from a string using the re module. The re.sub() is a built-in Python method that replaces one or many matches with a string.

import re

inputString = "Is your email id datascience_123@gmail.com"
outputString = re.sub(r"[^a-zA-Z0-9]", "", inputString)
print(outputString)

Output

Isyouremailiddatascience123gmailcom

The re.sub() method replaces the non-alphanumeric characters with empty strings. That way, we removed the non-alphanumeric characters.

Conclusion

In this article, we got to know how to remove all non-alphanumeric characters, including spaces from a string in Python using 

  • Using the filter(str.isalnum, inputString) function.
  • The isalnum() method returns True when all the characters in the string are alphabets and numbers and returns False when the string contains any special symbols or characters, including spaces.
  • Using the re.sub() function helps us check if a particular string matches according to the condition given.

That’s it for this tutorial.

Leave a Comment