Member-only story

Phone Number Handling in Python

Python Coding
2 min readAug 25, 2024

--

pip install phonenumbers

1. Parse and Format a Phone Number

This program parses a phone number and formats it in the international format.

import phonenumbers
from phonenumbers import PhoneNumberFormat, format_number

phone_number = phonenumbers.parse("+14155552671", "US")

formatted_number = format_number(phone_number, PhoneNumberFormat.INTERNATIONAL)

print(f"Formatted Number: {formatted_number}")
Formatted Number: +1 415-555-2671

2. Validate a Phone Number

This program checks whether a phone number is valid or not.

import phonenumbers

phone_number = phonenumbers.parse("+14155552671", "US")

is_valid = phonenumbers.is_valid_number(phone_number)

print(f"Is the phone number valid? {'Yes' if is_valid else 'No'}")
Is the phone number valid? Yes

3. Get the Location of a Phone Number

This program retrieves the location associated with a phone number.

import phonenumbers
from phonenumbers import geocoder…

--

--

Python Coding
Python Coding

Written by Python Coding

Learn python tips and tricks with code I Share your knowledge with us to help society. Python Quiz: https://www.clcoding.com/p/quiz-questions.html

Responses (1)