Python | Remove all characters except letters and numbers

14 Jun, 2022
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 request and response in HTTP queries are in the form of strings with sometimes some useless data which we need to remove. Let’s discuss some Pythonic ways to remove all the characters except numbers and alphabets. Method #1: Using re.sub 
Python3
# Python code to demonstrate# to remove all the characters# except numbers and alphabets import re # initialising stringini_string = "123abcjw:, .@! eiw" # printing initial stringprint ("initial string : ", ini_string) # function to demonstrate removal of characters# which are not numbers and alphabets using re result = re.sub('[\W_]+', '', ini_string) # printing final stringprint ("final string", result)
initial string :  123abcjw:, .@! eiw
final string 123abcjweiw
  Method #2: Using isalpha() and isnumeric() 
Python3
# Python code to demonstrate# to remove all the characters# except numbers and alphabets import re # initialising stringini_string = "123abcjw:, .@! eiw" # printing initial stringprint ("initial string : ", ini_string) # function to demonstrate removal of characters# which are not numbers and alphabets using regetVals = list([val for val in ini_string               if val.isalpha() or val.isnumeric()]) result = "".join(getVals) # printing final stringprint ("final string", result)
initial string :  123abcjw:, .@! eiw
final string 123abcjweiw
  Method #3: Using alnum() 
Python3
# Python code to demonstrate# to remove all the characters# except numbers and alphabets # initialising stringini_string = "123abcjw:, .@! eiw" # printing initial stringprint ("initial string : ", ini_string) # function to demonstrate removal of characters# which are not numbers and alphabets using regetVals = list([val for val in ini_string if val.isalnum()])result = "".join(getVals) # printing final stringprint ("final string", result)
initial string :  123abcjw:, .@! eiw
final string 123abcjweiw
Method#4: Using filter and in 
Python3
# Python code to demonstrate# to remove all the characters# except numbers and alphabets # initialising stringini_string = "123abcjw:, .@! eiw" # printing initial stringprint ("initial string : ", ini_string) k = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";# function to demonstrate removal of characters# which are not numbers and alphabets using filter and ingetVals = 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
satyam00so
Python Regex-programs
Python string-programs
python-regex
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
Python program to convert a list to string
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python Program for Fibonacci numbers