The Wayback Machine - https://web.archive.org/web/20230315005647/https://www.geeksforgeeks.org/python-remove-all-characters-except-letters-and-numbers/
Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Remove all characters except letters and numbers

Improve Article
Save Article
  • Last Updated : 14 Feb, 2023
Improve Article
Save Article

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 string
ini_string = "abcjw:, .@! eiw"
 
# printing initial string
print ("initial string : ", ini_string)
 
result = re.sub('[\W_]+', '', ini_string)
 
# printing final string
print ("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 string
ini_string = "123abcjw:, .@! eiw"
 
# printing initial string
print ("initial string : ", ini_string)
 
getVals = list([val for val in ini_string
            if val.isalpha() or val.isnumeric()])
 
result = "".join(getVals)
 
# printing final string
print ("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 string
ini_string = "123:, .@! "
 
# printing initial string
print ("initial string : ", ini_string)
 
getVals = list([val for val in ini_string if val.isalnum()])
result = "".join(getVals)
 
# printing final string
print ("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 string
ini_string = "123abcjw:, .@! eiw"
 
# printing initial string
print ("initial string : ", ini_string)
 
k = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
getVals = list(filter(lambda x: x in k, ini_string))
result = "".join(getVals)
 
# printing final string
print ("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 string
ini_string = "123abcjw:, .@! eiw"
 
# printing initial string
print ("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 string
print ("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 characters
allowed_chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
# initialising string
ini_string = "123abcjw:, .@! eiw"
 
# printing initial string
print("initial string : ", ini_string)
s = ""
for i in ini_string:
    if op.countOf(allowed_chars, i) > 0:
        s += i
# printing final string
print("final string", s)

Output

initial string :  123abcjw:, .@! eiw
final string 123abcjweiw

Time Complexity: O(N)

Auxiliary Space : O(N)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!