KnowledgeBoat Logo

Computer Science

Write a program that prompts for a phone number of 10 digits and two dashes, with dashes after the area code and the next three numbers. For example, 017-555-1212 is a legal input. Display if the phone number entered is valid format or not and display if the phone number is valid or not (i.e., contains just the digits and dash at specific places.)

Python

Python String Manipulation

37 Likes

Answer

phNo = input("Enter the phone number: ")
length = len(phNo)
if length == 12 \
    and phNo[3] == "-" \
    and phNo[7] == "-" \
    and phNo[:3].isdigit() \
    and phNo[4:7].isdigit() \
    and phNo[8:].isdigit() :
    print("Valid Phone Number")
else :
    print("Invalid Phone Number")

Output

Enter the phone number: 017-555-1212
Valid Phone Number

=====================================

Enter the phone number: 017-5A5-1212
Invalid Phone Number

Answered By

14 Likes


Related Questions