Phonenumbers module in Python
Last Updated :
05 Sep, 2020
Python is a very powerful language and also very rich in libraries. phonenumbers is one of the modules that provides numerous features like providing basic information of a phone number, validation of a phone number etc. Here, we will learn how to use phonenumbers module just by writing simple Python programs. This is a Python port of Google’s libphonenumber library.
Installation
Install the phonenumbers module by typing the following command in command prompt.
pip install phonenumbers
Getting Started
1. Convert String to phonenumber format: To explore the features of phonenumbers module, we need to take the phone number of a user in phonenumber format. Here we will see how to convert the user phone number to phonenumber format. Input must be of string type and country code must be added before phone number.
Python3
import phonenumbers
phoneNumber = phonenumbers.parse( "+919876543210" )
print (phoneNumber)
|
Output:
Country Code: 91 National Number: 9876543210
2. Get Timezone: Here is the simple Python program to get the timezone of a phone number using phonenumbers module. First, we do parse the string input to phonenumber format, and then we use an inbuilt function to get the timezone of a user. It gives the output for valid numbers only.
Python3
import phonenumbers
from phonenumbers import timezone
phoneNumber = phonenumbers.parse( "+919876543210" )
timeZone = timezone.time_zones_for_number(phoneNumber)
print (timeZone)
|
Output:
('Asia/Calcutta',)
3. Extract phone numbers from text: We can extract phone numbers that are present in a text/paragraph using this module. You can iterate over it to retrieve a sequence of phone numbers. For this, PhoneNumberMatcher object provides the relevant function.
Python3
import phonenumbers
text = "Contact us at +919876543210 or +14691234567"
numbers = phonenumbers.PhoneNumberMatcher(text, "IN" )
for number in numbers:
print (number)
|
Output:
PhoneNumberMatch [14,27) +919876543210
4. Carrier and Region of a Phone Number: Here we will learn how to find the carrier and region of a phone number using the geocoder and carrier functions of this module.
Python3
import phonenumbers
from phonenumbers import geocoder, carrier
phoneNumber = phonenumbers.parse( "+919876543210" )
Carrier = carrier.name_for_number(phoneNumber, 'en' )
Region = geocoder.description_for_number(phoneNumber, 'en' )
print (Carrier)
print (Region)
|
Output:
Airtel
India
5. Validation of a phone number: A simple python program, to check whether a given phone number is valid or not (e.g. it’s in an assigned exchange), and to check whether a given phone number is possible or not (e.g. it has the right number of digits).
Python3
import phonenumbers
phone_number = phonenumbers.parse( "+91987654321" )
valid = phonenumbers.is_valid_number(phone_number)
possible = phonenumbers.is_possible_number(phone_number)
print (valid)
print (possible)
|
Output:
False
True
Share your thoughts in the comments
Please Login to comment...