How to get Phone Number details using Python

In this article, we are going to learn how to get phone number details in Python. There is a very interesting and useful library with the name ‘phonenumbers’. We will be using this library in order to get the information regarding a phone number. Let’s dive further into this tutorial to know more about it.

How to get Phone Number details using Python

First, you need to install phonenumbers library using the following command in your command prompt.

pip install phonenumbers

Now see the below example program to understand its working.

import phonenumbers as pn
from phonenumbers import geocoder

phone = pn.parse('+919293949595')
print(geocoder.description_for_number(phone, 'en'))

Output:

India

In the above program, first, we imported the required libraries and then we used the parse() method to parse a valid phone number and store it in a phone variable. The method description_for_number() prints the name of the country, to which the parsed number belongs.

Get carrier name or network name from phone number in Python

Now see the below example where we have used the name_for_number() method to get the name of the service provider for the parsed phone number. Here goes the code.

import phonenumbers as pn
from phonenumbers import carrier

phone = pn.parse('+919293949595')
print(pn.is_valid_number(phone))
print(carrier.name_for_number(phone, 'en'))

Output:

True
Reliance Jio

The is_valid_number() method in the above example program checks if the parsed number is valid or not. After the verification, which returns True, it prints the name of the service provider company.

Thank you.

Also read: Faker Library in Python

Leave a Reply