How to Get Phone Number Information in Python?

FeaImg PhoneNo Details

In this tutorial, we’ll look at how to get phone number information in Python. The name ‘phonenumbers’ refers to an extremely fascinating and handy library. And that’s the library that will help us have some fun with Phone numbers in Python.

Steps to Get Phone Number Information in Python

We will be utilizing the phonenumbers library to obtain information about a phone number. Let’s go deeper into this lesson to learn more about it.

To begin, run the following line on your command prompt to install the phonenumbers library.

pip install phonenumbers

Convert String to phonenumber format

To investigate the functionalities of the phonenumbers module, we must first obtain a user’s phone number in phonenumber format. In this section, we’ll look at how to convert a user’s phone number to phonenumber format.

The input must be of the string type, and the country code must come before the phone number.

import phonenumbers
pN = phonenumbers.parse("+919876643290")
Country Code: 91 National Number: 9876643290

Get Timezone

Here is a simple Python program that uses the phonenumbers module to determine the timezone of a phone number.

First, we convert the string input to phone number format, and then we utilize an inbuilt method to determine a user’s timezone. It only returns results for valid numbers.

import phonenumbers
from phonenumbers import timezone
pN = phonenumbers.parse("+919876643290")
tZ = timezone.time_zones_for_number(pN)

Extract phone numbers from text

Using this module, we can extract phone numbers from text/paragraphs. You may iterate through it to get a list of phone numbers. The PhoneNumberMatcher object offers the necessary method for this.

import phonenumbers
T = "Contact us at +919876643290 or +14691674587"
N = phonenumbers.PhoneNumberMatcher(T, "IN")
for n in N:
PhoneNumberMatch [14,27) +919876643290

Carrier and Region of a Phone Number

We will learn how to use the geocoder and carrier functionalities of this module to determine the carrier and area of a phone number.

import phonenumbers
from phonenumbers import geocoder, carrier
pN = phonenumbers.parse("+919876643290")
C = carrier.name_for_number(pN, 'en')
R = geocoder.description_for_number(pN, 'en')


Congratulations! You just learned how to get Phone Number Information in Python. Hope you enjoyed it! 😇

Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:

  1. Python: Convert Number to Words
  2. Convert a number to words [digit by digit] in Python
  3. 3 Easy Methods to Find the Smallest Number in Python

Thank you for taking your time out! Hope you learned something new!! 😄