How to Get Phone Number Information Using Python

Hi there, today we will be talking about something which is very interesting as well as simple: Getting phone number information using Python. A few lines of code can be used to get all the information.

So let’s get started right away. 

Phone Number Information Project

Requirements:

Basic knowledge of python(Learn Python Tutorial) . We can use any ide ( for example: https://code.visualstudio.com/download , https://jupyter.org/) . 

Modules required :

Phonenumbers: We will be using this module for getting various information about any phone number (for examples:- Country code, validation of the number, Timezone, carrier and region of that number etc.). Phonenumbers is a Python port of Google’s libphonenumber library.

Installation :

For Installing the phonenumbers module ,we have to type the following command in command prompt.

pip install phonenumbers

Getting started:

1. Parsing:  Here we will see how to parse the user phone number to phonenumber format( (+Countrycode) xxxxxxxxxx ).

Firstly, we have to import the module . Secondly, we will write the parse() function .

Source Code:
 import phonenumbers
 PhoneNumber = phonenumbers.parse ("+919876543210")
 print(PhoneNumber) 

2. Getting the Timezone: In this section, we will write a small python script to get the timezone.

Firstly, we will import phonenumbers module and from phonenumbers module import timezone. Secondly, we will parse the number into phonenumber format and use the time_zones_for_number().

Source Code:
 import phonenumbers
 from phonenumbers import timezone
 phoneNumber = phonenumbers.parse("+919876543210")
 timeZone = timezone.time_zones_for_number(phoneNumber)
 print(timeZone) 

3. Name of the carrier: Here, we will learn how to use the carrier function of this module to get the carrier of the number.

Firstly, we have to import the modules. Secondly, we will parse the number and finally print the name of the carrier using carrier function.

Source Code:
 import phonenumbers
 from phonenumbers import carrier
 phoneNumber = phonenumbers.parse("+919876543210")
 Carrier = carrier.name_for_number(phoneNumber, 'en')
 print(Carrier) 

3. Region of the number : Here, we will learn how to use the geocoder function of this module to get the geocoder of the number.

Firstly, we have to import the modules. Secondly, we will parse the number  into phone number format and finally print the name of the region using geocoder function.

Source Code:
 import phonenumbers
 from phonenumbers import geocoder
 phoneNumber = phonenumbers.parse("+919876543210")
 Region = geocoder.description_for_number(phoneNumber, 'en')
 print(Region) 

4. Validation of a number : In this paragraph, we will see how we can check the validation amd possibility of any number.

Source Code:
 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) 

Hope you liked the tutorial.