Alphanumeric characters contains one mor more,
- Letters in lowercase i.e. a to z.
- Letters in uppercase i.e. A to Z.
- Numbers from 0 to 9.
In this article, we will discuss four different ways to remove all non alphanumeric characters from string. These ways are,
- Using Regex
- Using join()
- Using filter() and join()
- Using for loop
Let’s discuss them one by one,
Remove all non alphanumeric characters using regex
In Python, the regex module provides a function sub(), which replaces the characters of a string based on the matching regex pattern. The signature of sub() function is as follows,
sub(pattern, replacement_str, original_str)
We can use this to all non alphanumeric characters from a string. For this we need to pass a regex pattern that matches all characters except alphanumeric characters like r”[^A-Za-z0-9]+”. Also, as a replacement string we need to pass the empty string. For example,
Frequently Asked:
sample_str = "Test & [88]%%$$$#$%-+ String 90$" # Remove characters that are not letter or numbers pattern = r'[^A-Za-z0-9]+' sample_str = re.sub(pattern, '', sample_str) print(sample_str)
Output:
Test88String90
Here, the sub() function searched for all the non-alphanumeric characters and then replaced them with the empty string. Then finally returned a copy of original string but with only alphanumeric characters. We assigned back this new string back to original variable, it gave an effect that we have delete all non-alphanumeric characters from the string.
Remove all non alphanumeric characters using join() is & isalpha()
In Python, string also provides a function isalpha(). Which returns True if all the characters in calling string object are alphanumeric. We can use this function along with the join() function. So, to remove all non alphanumeric characters from a string, we will iterate over all characters of string one by one and skip the non-alphanumeric characters. Then using the join() function, we will combine the remaining characters. For example,
sample_str = "Test & [88]%%$$$#$%-+ String 90$" # Remove all non alpha-numeric characters from a string sample_str = ''.join(item for item in sample_str if item.isalnum()) print(sample_str)
Output:
Latest Python - Video Tutorial
Test88String90
It deleted all non-alphanumeric characters from string.
Remove all non alphanumeric characters using filter(), join() and isalpha()
We can use the filter() function to filter all non-alphanumeric characters from a string. Steps are as follows,
- Pass the isalpha() function as the conditional argument to filter() function, along with the string to be modified.
- filter() function yields only those characters from given string for which isalpha() returns True i.e. only alphanumeric characters.
- Use join() function to combine all those characters which are yielded by the filter() function i.a. only alphanumeric characters.
- Assign back the string returned by join() function to original variable. It will give an effect the we have deleted all non alphanumeric characters.
For example,
sample_str = "Test & [88]%%$$$#$%-+ String 90$" # Filter only alpha-numeric characters from a string sample_str = ''.join(filter(str.isalnum, sample_str)) print(sample_str)
Output:
Test88String90
It deleted all non-alphanumeric characters from string.
Remove all non alphanumeric characters from string using for loop
Create a new empty temporary string. Then iterate over all characters in string using a for loop and for each character check if it is alphanumeric or not. If it is alphanumeric, then append it to temporary string created earlier. When the for loop ends, the temporary string contains only the alphanumeric characters from original string. Assign temporary string to original variable. It will give an effect the we have deleted all non alphanumeric characters. For example,
sample_str = "Test & [88]%%$$$#$%-+ String 90$" # Iterate over all characters in string using a for loop # and select only those characters, which are alpha-numberic mod_string = "" for elem in sample_str: if elem.isalnum(): mod_string += elem sample_str = mod_string print(sample_str)
Output:
Test88String90
It deleted all non-alphanumeric characters from string.
Remove all non alphanumeric characters from string except space
We will use the logic explained in above example i.e. iterate over all characters of string using for loop. Pick only alphanumeric characters and space. For example,
sample_str = "Test & [88]%%$$$#$%-+ String 90$" # Iterate over all characters in string using a for loop # and select only those characters, which are alpha-numberic or space mod_string = "" for elem in sample_str: if elem.isalnum() or elem == ' ': mod_string += elem sample_str = mod_string print(sample_str)
Output:
Test 88 String 90
It deleted all non-alphanumeric characters from string except space.
Summary:
We learned about different ways to delete all non-alphanumeric characters from a string in python.
Latest Video Tutorials