Do you want to remove non-alphanumeric characters from strings in Python? This article will show you how you can remove non-alphanumeric characters using 3 different methods.
In this case either you can use a regex library to replace all the non-alphanumeric characters or to retain the alphanumeric characters. And the second method you can use is the join Method of String Class in which you can skip the non-alphanumeric characters while joining.
Remove Non-Alphanumeric Character From String Using Regex
Using Regex Library and its sub-function you can replace the non-alphanumeric characters and get an alphanumeric string as output.
This method takes three inputs arguments in the first input argument you need to mention all the non-alphanumeric characters you want to skip and the second argument is what you want to replace those non-alphanumeric characters with.
And the third argument is the given string itself on which you want to perform the substitution.
Let us see the usage of regex and sub to get alphanumeric character strings using Python.
#importing Regex Library import re #Initializing String with non-alphanumeric Characters. givenString = ",@#$(@*COD&U*/BER" cleanString = re.sub('[\W_]', '', givenString) print(cleanString)
Remove Non-Alphanumeric Character From String Using Join and isalpha Function
You can use the join function from the string class as well to remove the non-alphanumeric characters while joining. Let us see in the below example code how you can use the join method to solve this problem.
Note that here we are also using the
isalpha() a method that checks whether the character is alphanumeric or not.
#Initializing String with non-alphanumeric Characters. givenString = ",@#$(@*COD&U*/BER" cleanString = ''.join(charac for charac in givenString if charac.isalpha()) print(cleanString)
Using Filter and isalpha
You can also use the filter method with
isalpha method to filter all the non-alphanumeric characters from the string. Let us see in the below example code the usage of filter in Python.
import string #Initializing String with non-alphanumeric Characters. givenString = "@#$(@*COD&U*/BER" cleanString = ''.join(x for x in list(filter(str.isalnum, givenString))) print(cleanString)
The above code does not add any additional feature that the one discussed above and hence this one is not recommended to use.
Using Regex and Pattern to Remove non-alphanumeric Characters From String
Using regex you can declare a pattern variable that will store all the non-alphanumeric patterns and then call the sub-method to remove those non-required characters. Let us see in the below example code to understand the usage.
#importing Regex Library import re #Initializing String with non-alphanumeric Characters. givenString = ",@#$(@*COD&U*/BER" #compiling here to get faster code. pattern = re.compile('[\W]') #Clearing the non-alphanumeric characters cleanString = pattern.sub('', givenString) #print the clean String print(cleanString)
Note that this is the fastest method to use in terms of execution. And if you have a big string or long list of such strings and want the conversion to be faster then you this method.
I hope you were able to get your answer to the above problem. Let me know if you have or know the any better method in the comment section I will be happy to add it here.
- How To Multiply Without Using * In Python
- How To Add or Append Values To a Set In Python
- 4 Ways To List All Subdirectories in a Directory – Python
- Dict To list – Convert A Dictionary to a List in Python
- Discord.py Bot Add Reaction To a Message Python
- Python Get The Minimum Value In Dictionary with Key
- How To Append Multiple Values To A List in Python