Python | Remove all characters except letters and numbers
Given a string, the task is to remove all the characters except numbers and alphabets. String manipulation is a very important task in a day to day coding and web development. Most of the requests and responses in HTTP queries are in the form of Python strings with sometimes some useless data which we need to remove.
Remove all characters except letters using re.sub
In this function, we demonstrate the removal of characters that are not alphabets using re.sub.
Python3
import re# initialising stringini_string = "abcjw:, .@! eiw"# printing initial stringprint ("initial string : ", ini_string)result = re.sub('[\W_]+', '', ini_string)# printing final stringprint ("final string", result) |
Output:
initial string : abcjw:, .@! eiw final string abcjweiw
Time complexity: O(n), where n is the length of the input string “ini_string”.
Auxiliary space: O(1), as only a constant amount of extra space is used for storing the final string “result”.
Remove all characters except letters and numbers using isalpha() and isnumeric()
Here, this function demonstrates the removal of characters that are not numbers and alphabets using isalpha() and isnumeric().
Python3
import re# initialising stringini_string = "123abcjw:, .@! eiw"# printing initial stringprint ("initial string : ", ini_string)getVals = list([val for val in ini_string if val.isalpha() or val.isnumeric()])result = "".join(getVals)# printing final stringprint ("final string", result) |
Output:
initial string : 123abcjw:, .@! eiw final string 123abcjweiw
Time Complexity: O(N)
Auxiliary Space: O(N)
Remove all characters except letters using alnum()
Here, this function demonstrates the removal of characters that do not number using alnum()
Python3
# initialising stringini_string = "123:, .@! "# printing initial stringprint ("initial string : ", ini_string)getVals = list([val for val in ini_string if val.isalnum()])result = "".join(getVals)# printing final stringprint ("final string", result) |
Output:
initial string : 123abcjw:, .@! eiw final string 123
Time Complexity: O(N)
Auxiliary Space: O(N)
Remove all characters except letters and numbers using a filter and in
Here, this function demonstrates the removal of characters that are not numbers and alphabets using a filter.
Python3
# initialising stringini_string = "123abcjw:, .@! eiw"# printing initial stringprint ("initial string : ", ini_string)k = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";getVals = list(filter(lambda x: x in k, ini_string))result = "".join(getVals)# printing final stringprint ("final string", result) |
Output:
initial string : 123abcjw:, .@! eiw final string 123abcjweiw
Time Complexity: O(N)
Auxiliary Space: O(N)
Remove all characters except letters and numbers using ord() function
Here, this function demonstrates the removal of characters that are not numbers and alphabets using ord() function.
Python3
# initialising stringini_string = "123abcjw:, .@! eiw"# printing initial stringprint ("initial string : ", ini_string)s=""for i in ini_string: if ord(i) in range(48,58) or ord(i) in range(65,91) or ord(i) in range(97,123): s+=i# printing final stringprint ("final string", s) |
Output
initial string : 123abcjw:, .@! eiw final string 123abcjweiw
Time Complexity: O(N)
Auxiliary Space: O(N)
Remove all characters except letters and numbers Using operator.countOf() method
Python3
import operator as op# create a string of allowed charactersallowed_chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"# initialising stringini_string = "123abcjw:, .@! eiw"# printing initial stringprint("initial string : ", ini_string)s = ""for i in ini_string: if op.countOf(allowed_chars, i) > 0: s += i# printing final stringprint("final string", s) |
initial string : 123abcjw:, .@! eiw final string 123abcjweiw
Time Complexity: O(N)
Auxiliary Space : O(N)
Remove all characters except letters and numbers Using Numpy
Note: Install numpy using command “pip install numpy” before running below code
Algorithm:
- Initialize the input string ini_string.
- Convert the input string to a numpy chararray object char_array.
- Extract only alphanumeric characters from char_array using numpy.char.isalnum() and store them in a new numpy array alphanumeric_chars.
- Convert alphanumeric_chars to a string using decode().
- Print the final string result.
Python3
import numpy as np # initialising stringini_string = "123abcjw:, .@! eiw" # printing initial stringprint ("initial string : ", ini_string) # create a char array from input stringchar_array = np.fromiter(ini_string, dtype='U1', count=len(ini_string)) # extact alphanumeric charactersalphanumeric_chars = char_array[np.char.isalnum(char_array)] # join alphanumeric characters into a stringresult = ''.join(alphanumeric_chars) # printing final stringprint ("final string", result) |
Output: initial string : 123abcjw:, .@! eiw final string 123abcjweiw
Time Complexity: O(n), where n is the length of the input string ini_string.
Auxiliary Space: O(n), as we are creating a numpy chararray object char_array and a numpy array alphanumeric_chars of the same size as char_array. Additionally, we are also creating a new string result of size n.


Please Login to comment...