Skip to content
Related Articles

Related Articles

Python | Remove all characters except letters and numbers

View Discussion
Improve Article
Save Article
  • Last Updated : 12 Sep, 2022
View Discussion
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

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

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

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

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!