How do we remove or replace some other special characters like alphabets and punctuations from a string in Python? I have thought to check for the special characters using string.ascii_letters
and string.punctuation
but, it haven't worked for me. I want only numbers from 0 to 9 to be taken, rather if user enter a special character then it needs to replaced with empty (Like this--> ''). So, is there any way to replace them?
Here is my Python code:
import string
mobile_no = '12aA%!@h34567890'
alpha_characters = list(string.ascii_letters)
special_characters = list(string.punctuation)
if alpha_characters in mobile or special_characters in mobile:
corrected = mobile_no.replace(alpha_characters, '')
corrected = mobile_no.replace(special_characters, '')
print(corrected)
Please correct me, if I'm wrong