Last active
August 23, 2023 11:55
-
-
Save Codehunter-py/e138c804067fab7f9740e6507891dc4e to your computer and use it in GitHub Desktop.
The convert_phone_number function checks for a U.S. phone number format: XXX-XXX-XXXX (3 digits followed by a dash, 3 more digits followed by a dash, and 4 digits), and converts it to a more formal format that looks like this: (XXX) XXX-XXXX.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| def convert_phone_number(phone): | |
| 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.")) # My number is (212) 345-9999. | |
| print(convert_phone_number("Please call 888-555-1234")) # Please call (888) 555-1234 | |
| print(convert_phone_number("123-123-12345")) # 123-123-12345 | |
| print(convert_phone_number("Phone number of Buckingham Palace is +44 303 123 7300")) # Phone number of Buckingham Palace is +44 303 123 7300 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you for both the code 👍 and clear explanation ~