The regular expression that goes in the given blank is r"\b(\d{3})-(\d{3})-(\d{4})\b",r"(\1) \2-\3",phone
Code:
import re
def convert_phone_number(phone):
# The pattern r"\b(\d{3})-(\d{3})-(\d{4})\b" checks for 3 digits
followed by a dash, followed by 3 more digits and a dash
# followed by 4 digits.
# r"(\1) \2-\3" indicates to enclose the first 3 digits of the
above pattern in paranthesis followed by a space,
# followed by the next three digits and a dash which is followed by
the remaining four digits.
result = re.sub(r"\b(\d{3})-(\d{3})-(\d{4})\b",r"(\1)
\2-\3",phone)
return result
print(convert_phone_number("My number is 212-345-9999."))
print(convert_phone_number("Please call 888-555-1234"))
print(convert_phone_number("123-123-12345"))
print(convert_phone_number("Phone number of Buckingham Palace is
+44 303 123 7300"))
Code in the image:
Output:
Explanation:
re.sub(r"\b(\d{3})-(\d{3})-(\d{4})\b",r"(\1) \2-\3",phone)
b(\d{3})-(\d{3})-(\d{4})\b matches the pattern starting with 3 digits followed by a dash, three more digits followed by a dash and then four digits.
(\1) \2-\3 this is used to format the contents of the above matched pattern. The first three digits are enclosed within paranthesis followed by a space, the next three digits followed by a dash and the next four digits.
phone is the parameter in which the above pattern has to be checked and formatted.
The convert_phone_number function checks for a U.S. phone number format: XXX-XXX-XXXX (3 digits followed by a...
This is Python
The program should accept input from the user as either 7-digit
phone number or 10-digit. If the user enters 7 characters, the
program should automatically add "512" to the beginning of the
phone number as the default area code. Dash (hyphen) characters do
not count in input character count, but must not be random. If the
user enters dashes the total character count must not exceed
12.
The program should not crash if the user enters invalid...
C++. You are to write a program to set up a data base for a phone index. The key will be a person’s name and the table will supply the phone number. The data base must be implemented as a hash table with the names as the key. The format will be a person name (multiple parts) followed by a phone number. Example: John Doe 601-5433 or O E Vay 921-1234. Only one name one phone number will be on...
6174 is known as Kaprekar's constant after the Indian mathematician D. R. Kaprekar. This number is notable for the following rule: 1. Take any four-digit number, using at least two different digits. 2. Arrange the digits in descending and then in ascending order to get two four-digit numbers, adding leading zeros if necessary. 3. Subtract the smaller number from the bigger number. 4. Go back to step 2 and repeat. The above process, known as Kaprekar's routine, will always reach...