Question

The convert_phone_number function checks for a U.S. phone number format: XXX-XXX-XXXX (3 digits followed by a dash, 3 more di

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

import re def convert_phone_number(phone): # The pattern rib(\d{3})-(16{3})-(\d{4})\b checks for 3 digits followed by a dash

Output:

(base) sunny@a4ron-5tone:-/Desktop$ python code.py My number is (212) 345-9999. Please call (888) 555-1234 123-123-12345 Phon

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.

Add a comment
Know the answer?
Add Answer to:
The convert_phone_number function checks for a U.S. phone number format: XXX-XXX-XXXX (3 digits followed by a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is Python The program should accept input from the user as either 7-digit phone number...

    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...

    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...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT