Introduction
Python comes with a module named phonenumbers which is used to get some basic information about a phone number; for instance, the carrier name, country name, timezone, validation of a phone number, etc.
In this tutorial, you will learn how to get phone number information using python programs.
Requirement and Installation
Install pip3 instead of pip for Linux.
Install phonenumbers: pip install phonenumbers
Make sure you've installed this module on your computer unless the programs will not work.
Check a phone number is valid✔️ or not🚫
In this section, we will check the validation of a phone number. For example, if someone gives an invalid phone number then the program will help to detect that.
As you know, USA🇺🇸 country code is 1, and the USA phone numbers are 11 digits in total. Suppose someone gives input "+1555333243" which consists of 10 digits, then the function will return a false message.
Code
import phonenumbers
phonenumber = input("Enter your mobile no. along with the
country code: ")
phonenumber = "+" + phonenumber
contact_number = phonenumbers.parse(phonenumber)
valid = phonenumbers.is_valid_number(contact_number)
if valid:
print("The phone number is valid")
else:
print("The phone number is not a valid")
# Check if the phone number is possible
# or not along with country code
possible = phonenumbers.is_possible_number(contact_number)
if possible:
print("The phone number is valid")
else:
print("The phone number is not matching perfectly")
Output
Enter your mobile no. along with the country code: 1555333243
The phone number is not a valid
The phone number is not matching perfectly
Get some Basic Information
Now, try to get some basic information about a phone number. For instance, the Area, Carrier Name, Time Zone, etc.
Code
import phonenumbers
from phonenumbers import geocoder
from phonenumbers import carrier
from phonenumbers import timezone
phonenumber = input("Enter your mobile no. along with the
country code: ")
phonenumber = "+" + phonenumber
contact_number = phonenumbers.parse(phonenumber)
# 'en' for english language
print(f"Area: {geocoder.description_for_number(contact_number,'en')}")
print(f"Carrier: {carrier.name_for_number(contact_number,'en')}")
print(f"Timezone: {timezone.time_zones_for_number(contact_number)}")
Output
Enter your mobile no. along with the country code: 917044719292
Area: India
Carrier: Airtel
Timezone: ('Asia/Calcutta',)
Format phone numbers in a standardized format
Here, we will differentiate a phone number in a unique format, nationally, internationally, and globally.
Code
import phonenumbers
phonenumber = input("Enter your mobile no. along with the
country code: ")
phonenumber = "+" + phonenumber
num = phonenumbers.parse(phonenumber)
int_ = phonenumbers.format_number(num, \
phonenumbers.PhoneNumberFormat.INTERNATIONAL)
nat_ = phonenumbers.format_number(num, \
phonenumbers.PhoneNumberFormat.NATIONAL)
glob = phonenumbers.format_number(num, \
phonenumbers.PhoneNumberFormat.E164)
print(f"International Format: {int_}")
print(f"National Format: {nat_}")
print(f"Unique Format: {glob}")
Output
Enter your mobile no. along with the country code: 12077233245
International Format: +1 207-723-3245
National Format: (207) 723-3245
Unique Format: +12077233245
Extract phone number from a text
Here we'll try to find phone numbers from a text.
Code
import phonenumbers
text = "Hi sam, It's Subhankar, and here are my \
contact numbers: +917044719292 and +12077233245"
for num in phonenumbers.PhoneNumberMatcher(text, "US"):
print(num)
for num in phonenumbers.PhoneNumberMatcher(text, "US"):
print(phonenumbers.format_number(num.number, \
phonenumbers.PhoneNumberFormat.E164))
Output
PhoneNumberMatch [55,68) +917044719292
PhoneNumberMatch [73,85) +12077233245
+917044719292
+12077233245