Python Program to Get Phone Number Information

In this post, you will learn how to use a simple Python programme to obtain information about phone numbers such as the country name to which the phone number belongs or the network service provider name of that phone number. For this task, Python provides a module called phonenumbers. This article describes Python’s implementation of Google’s library phonenumber module.

Installation:

pip install phonenumbers

Program to Get Phone Number Information in Python

1)Converting String to Phone Number Format

To study the phonenumbers module’s functionality, we must first collect a user’s phone number in phonenumber format. Let us look at how to convert a user’s phone number to phone number format in this part.

The input must be of the string type, with the country code preceding the phone number.

Approach:

  • Import phonenumbers module using the import keyword.
  • Pass some random phone number along with the country code to the parse() function and store it in a variable.
  • Print the above-given phone number.
  • The Exit of the Program.

Below is the implementation:

# Import phonenumbers module using the import keyword
import phonenumbers
# Pass some random phone number along with the country code to the parse() function and 
# store it in a variable.
phno= phonenumbers.parse("+919494580181")
# Print the above given phone number
print(phno)

Output:

Country Code: 91 National Number: 9494580181

2)Python Code for getting the TimeZone

The below is the simple Python code that uses the phonenumbers module to detect a phone number’s timezone.

We first convert the string input to phone number format, and then we use a built-in method to detect the timezone of the user. It only gives results for numbers that are valid.

Approach:

  • Import phonenumbers module using the import keyword.
  • Import timezone function from phonenumbers module using the import keyword.
  • Pass some random phone number along with the country code to the parse() function and store it in a variable.
  • Pass the above phone number to the time_zones_for_number() function to get the timezone of the given number.
  • Store it in another variable.
  • Print the timezone of the given phone number.
  • The Exit of the Program.

Below is the implementation:

# Import phonenumbers module using the import keyword
import phonenumbers
# Import timezone function from phonenumbers module using the import keyword
from phonenumbers import timezone
# Pass some random phone number along with the country code to the parse() function and 
# Store it in a variable.
phno= phonenumbers.parse("+919494580181")
# Pass the above phone number to the time_zones_for_number() function 
# to get the timezone of the given number.
# Store it in another variable.
TimeZone = timezone.time_zones_for_number(phno)
# Print the timezone of the given phone number.
print("The timezone of the given phone number is:")
print(TimeZone)

Output:

The timezone of the given phone number is:
('Asia/Calcutta',)

3)Python Code for extraction of Phone Numbers from Text 

We may extract phone numbers from text/paragraphs using this module. You can go through it several times to generate a list of phone numbers. The PhoneNumberMatcher object provides the required function for this.

Approach:

  • Import phonenumbers module using the import keyword.
  • Give some random text as static input and store it in a variable.
  • Pass the above text and Indian Country code as arguments to the PhoneNumberMatcher() function to extract the Indian phone numbers from the given text.
  • Store it in another variable.
  • Iterate in the above phone numbers list using the for loop.
  • Print the phone number.
  • The Exit of the Program.

Below is the implementation:

# Import phonenumbers module using the import keyword
import phonenumbers
# Give some random text as static input and store it in a variable.
gvn_text= "some random phone numbers are +919876787098 or +14394674587"
# Pass the above text and Indian Country code as arguments to the PhoneNumberMatcher() 
# function to extract the Indian phone numbers from the given text.
# Store it in another variable.
phnos = phonenumbers.PhoneNumberMatcher(gvn_text, "IN")
# Iterate in the above phone numbers list using the for loop
for i in phnos:
    # Print the phone number
    print(i)

Output:

PhoneNumberMatch [30,43) +919876787098

4)Python Code for getting Carrier and Area/Region of a Phone Number

The geocoder and carrier functions of the phonenumbers module are used to get the carrier(network type)and region of a phone number.

Approach:

  • Import phonenumbers module using the import keyword.
  • Import geocoder, carrier functions from phonenumbers module using the import keyword.
  • Pass some random phone number along with the country code to the parse() function and store it in a variable.
  • Pass the above phone number and ‘en’ as arguments to the carrier.name_for_number() function to get the carrier(network) of the given phone number.
  • Store it in another variable.
  • Pass the above phone number and ‘en’ as arguments to the geocoder.description_for_number() function to get the area or region of the given phone number.
  • Store it in another variable.
  • Print the carrier(network) of the given phone number.
  • Print the area or region of the given phone number.
  • The Exit of the Program.

Below is the implementation:

# Import phonenumbers module using the import keyword
import phonenumbers
# Import geocoder, carrier functions from phonenumbers module using the import keyword
from phonenumbers import geocoder, carrier
# Pass some random phone number along with the country code to the parse() function and 
# Store it in a variable.
phno= phonenumbers.parse("+919494580181")
# Pass the above phone number and 'en' as arguments to the carrier.name_for_number() function
# to get the carrier(network) of the given  phone number.
# Store it in another variable.
phno_Carrier = carrier.name_for_number(pN, 'en')
# Pass the above phone number and 'en' as arguments to the geocoder.description_for_number() function
# to get the area or region of the given phone number.
# Store it in another variable.
phno_area= geocoder.description_for_number(pN, 'en')
# Print the carrier(network) of the given phone number.
print("The carrier(network) of the given phone number = ", phno_Carrier)
# Print the area or region of the given phone number.
print("The area or region of the given phone number = ",phno_area)

Output:

The carrier(network) of the given phone number = BSNL MOBILE 
The area or region of the given phone number = India