Introduction
In this tutorial, we will be looking at how to get information about any phone number using python. To begin we will first need to install a module called phonenumbers. To install it run the command below.
Table of Contents
Install module
Open your terminal and run the code below.
pip3 install phonenumbers
Create app
Now let’s create a file called app.py and import these modules as shown below.
import phonenumbers
from phonenumbers import geocoder, carrier
Now let’s create a class that will get different information from our phone number such as the phone carrier, region, and validity of the phone number.
class PhoneNumberInfo:
def __init__(self, number):
self.number = number
self.parsed_number = phonenumbers.parse(number, None)
def is_valid_number(self):
return phonenumbers.is_valid_number(self.parsed_number)
def get_region(self):
return geocoder.description_for_number(self.parsed_number, "en")
def get_carrier(self):
return carrier.name_for_number(self.parsed_number, "en")
if __name__ == '__main__':
number = "+254103425308"
phone_number_info = PhoneNumberInfo(number)
print(phone_number_info.parsed_number)
print("Valid : ", phone_number_info.is_valid_number())
print("Region : ", phone_number_info.get_region())
print("Carrier : ", phone_number_info.get_carrier())
Run
Open your terminal and run the command below.
python3 app.py
You should now get your results displayed on the terminal. To get more information about a phone number visit here.
Thanks for reading.