Find country of a phone number from country code in Python

In this tutorial, we are going to learn how can we Find the country of a phone number from the country code in Python. It’s very simple and easy just a few lines of code. let’s see how it works.

For finding the country respective to their phone number attached with the phone code we are going to use the python module called phonenumbers. Phonenumbers module is used for finding basic information of phone number, carrier, region, etc.

For this first, we have to install this module to our system. We can do this by using the pip command as shown below.

pip install phonenumbers

Next, we are going to import that module and we are going to use some functions of it. The full code is given below.

#importing module
import phonenumbers as pn
from phonenumbers import geocoder

#input phone number with country code
ph_number=input("Enter Phone number with Country code: ")
ph_number=pn.parse(ph_number)

#printing the asscociated country with phone number
print(geocoder.description_for_number(ph_number,"en"))

Here we are importing phonenumbers as pn. From phonenumbers, we are importing geocoder. Geocoder is used for retrieving country names according to phone number code.

Next, we are giving phone numbers with country code as input to the ph_number variable. The parse function is used to process that number. And finally using the description_for_number function we are printing the country of that Phone number.

See what the output looks like.

Output

Enter Phone number with Country code: +97710589678 
Nepal

Also read: How to get Phone Number details using Python

Leave a Reply

Your email address will not be published.