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
Write a program which replaces all vowels in the string with '*'.
Write a program that should prompt the user to type some sentence(s) followed by "enter". It should then print the original sentence(s) and the following statistics relating to the sentence(s) :
- Number of words
- Number of characters (including white-space and punctuation)
- Percentage of characters that are alphanumeric
Hints
- Assume any consecutive sequence of non-blank characters is a word.
Write a program which reverses a string and stores the reversed string in a new string.
Write a program that should do the following :
- prompt the user for a string
- extract all the digits from the string
- If there are digits:
- sum the collected digits together
- print out the original string, the digits, the sum of the digits
- If there are no digits:
- print the original string and a message "has no digits"
Sample
- given the input : abc123
prints abc123 has the digits 123 which sum to 6 - given the input : abcd
prints abcd has no digits