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)


Please Login to comment...