Skip to Content

Python – Remove Non Alphanumeric Characters from String

In this tutorial, we will look at how to remove non-alphanumeric characters from a string in Python with the help of some examples.

A character is an alphanumeric character if it’s either an alphabet (a to z, A to Z) or a digit (0 to 9). For example, the string striker123 contains only alphanumeric characters whereas the string striker_123 contains one non-alphanumeric character ('_').

Remove non alphanumeric characters from string

You can use the string isalnum() function to check if a character is an alphanumeric character or not.

# check if character is alphanumeric
print('a'.isalnum())
print('A'.isalnum())
print('7'.isalnum())
print('_'.isalnum())
print('#'.isalnum())

Output:

True
True
True
False
False

Removing non alphanumeric characters from a string is commonly used as a text preprocessing step. Let’s now look at how to remove non alphanumeric characters from a string with the help of some examples.

Highlighted programs for you

Flatiron School

Flatiron School

Data Science Bootcamp
Product Design UX/UI Bootcamp

University of Maryland Global Campus

University of Maryland Global Campus

Cloud Computing Systems Master's
Digital Forensics & Cyber Investigation Master's

Creighton University

Creighton University

Health Informatics Master's

There are a number of ways you can remove non alphanumeric characters from a string in Python.

You can use the string isalnum() function along with the string join() function to create a string with only alphanumeric characters.

# string with non alphanumeric characters
s = "Striker@#$_123"
# remove non alphanuemeric characters
new_s = ''.join(c for c in s if c.isalnum())
print(new_s)

Output:

Striker123

You can see that the resulting string doesn’t have any non alphanumeric characters. Here we iterate over all the characters in the original string and keep it only if it’s an alphanumeric character which we check using the string isalnum() function. We then use the string join() function to concatenate each character.

We can also use regular expressions to remove such characters. For example, we can write a regular expression to match with all the non-alphanumeric characters in the string and then replace them with an empty string. You can use the re library in Python to implement regular expression pattern matching.

import re

# string with non alphanumeric characters
s = "Striker@#$_123"
# remove non alphanuemeric characters
new_s = re.sub(r'[^a-zA-Z0-9]', '', s)
print(new_s)

Output:

Striker123

We get the same result as above.


For more on regular expressions in Python, refer to this guide.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.