Write a program that asks a user to enter his/her phone number. Then remove all the non-digits from the phone number and then display the digits entered. Hint: you may need to use the syntax “.isdigit()” in your program
Sample run
Please enter your phone number?123-456/7891
1234567891
Please enter your phone number?123(456)-7891
1234567891
Please enter your phone number?987@654!4321
9876544321
The Solution:
#accept the phone number as input
phone_number=input("Please enter your phone number?")
#temp variable to store only digits
only_digits=''
#for loop to interate through the input phone number
for i in phone_number:
#check whether it is digit
if(i.isdigit()):
#if true, add it to temp variable
only_digits+=i
else:
#if false, continue to skip the character
continue
#print only the digits
print(only_digits)
The Screenshots:
The Code:
The Output:
Get Answers For Free
Most questions answered within 1 hours.