
Last Updated on 2 May 2025
Python comes with a module named phonenumbers, which is used to get some basic information about a phone number; for example, the carrier name, country name, timezone, validation of a phone number, etc.
In this tutorial, we’ll create various Python scripts to get phone number information and track mobile numbers using the phonenumbers module. We will find:
- ✅ Validation of phone numbers (is the number real?)
- 📍 Location, carrier, and timezone info
- 🔢 Format phone numbers
- 📑 Phone numbers from text (Extraction)
At the end, you’ll also learn how to use folium and opencage to display the phone number’s region on a map.
Prerequisites
Install the following requirements:
pip install phonenumbers pip install folium pip install opencage
Check if a phone number is valid or not
In this section, we will check the validation of a phone number. For example, if someone gives an invalid phone number, then the program will detect that.
For example, the USA🇺🇸 country code is 1, and a USA phone number might look like +12125554567, in total 11 digits (10-digit phone number, 1-digit country code). Suppose someone gives input “+1555333243”, which consists of 10 digits in total, then our program will detect an error in that number.
import phonenumbers
phonenumber = input("Enter mobile number with country code (e.g. 12125554567): ")
phonenumber = "+" + phonenumber
number_obj = phonenumbers.parse(phonenumber)
# Check if it's a valid number
if phonenumbers.is_valid_number(number_obj):
print("✔️ The phone number is valid.")
else:
print("🚫 The phone number is not valid.")
# Check if it's a possible number
if phonenumbers.is_possible_number(number_obj):
print("✔️ The phone number is possible.")
else:
print("⚠️ The phone number is not possible.")
Output
Enter mobile number with country code (e.g. 12125554567): 12124567890 ✔️ The phone number is valid. ✔️ The phone number is possible.
Enter mobile number with country code (e.g. 12125554567): 918957233713 ✔️ The phone number is valid. ✔️ The phone number is possible.
In the first example, we entered a 10-digit phone number ‘2124567890’ along with the country code ‘1’.
In the second example, we entered a 10-digit Indian phone number ‘8957233713’ along with the country code ’91’. Our program identified it correctly.
Find Some Basic Information
Now, we will find some basic information about a phone number. For example, the Area, Carrier Name, Time Zone, etc.
import phonenumbers
from phonenumbers import geocoder, carrier, timezone
phonenumber = input("Enter mobile number with country code: ")
phonenumber = "+" + phonenumber
number_obj = phonenumbers.parse(phonenumber)
print("📍 Region:", geocoder.description_for_number(number_obj, 'en'))
print("📱 Carrier:", carrier.name_for_number(number_obj, 'en'))
print("⏰ Time Zone:", timezone.time_zones_for_number(number_obj))
Output
Enter mobile number with country code: 1212-456-7890
📍 Region: New York, NY
📱 Carrier:
⏰ Time Zone: ('America/New_York',)
Enter mobile number with country code: 917044719292
📍 Region: India
📱 Carrier: Airtel
⏰ Time Zone: ('Asia/Calcutta',)
Format phone numbers in a standardized format
You can convert any phone numbers into international, national, or E.164 format using this Python script:
import phonenumbers
phonenumber = input("Enter mobile number with country code: ")
phonenumber = "+" + phonenumber
number_obj = phonenumbers.parse(phonenumber)
print("🌐 International Format:", phonenumbers.format_number(number_obj, phonenumbers.PhoneNumberFormat.INTERNATIONAL))
print("🏠 National Format:", phonenumbers.format_number(number_obj, phonenumbers.PhoneNumberFormat.NATIONAL))
print("🔐 E.164 Format:", phonenumbers.format_number(number_obj, phonenumbers.PhoneNumberFormat.E164))
Output
Enter mobile number with country code: 12077233245 🌐 International Format: +1 207-723-3245 🏠 National Format: (207) 723-3245 🔐 E.164 Format: +12077233245
Note: E.164 is a strict, machine-friendly version of the international format.
Extract Phone Numbers from a Text
You can scan and extract all phone numbers from a block of text.
import phonenumbers
text = "Hi Sam, call me at +917044719292 or on my US number +12077233245."
matches = phonenumbers.PhoneNumberMatcher(text, "US")
for match in matches:
number = phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.E164)
print("📞 Found number:", number)
Output
📞 Found number: +917044719292 📞 Found number: +12077233245
Track Phone Number Region on a Map
Let’s do something interesting. Here, we will plot the region of an entered phone number on a map using folium and opencage.
🔑 You need to get a free API key from https://opencagedata.com to use their geocoding service.
import phonenumbers
from phonenumbers import geocoder
from opencage.geocoder import OpenCageGeocode
import folium
key = 'YOUR_OPENCAGE_API_KEY'
phonenumber = input("Enter mobile number with country code: ")
phonenumber = "+" + phonenumber
number_obj = phonenumbers.parse(phonenumber)
location = geocoder.description_for_number(number_obj, 'en')
geocoder_api = OpenCageGeocode(key)
results = geocoder_api.geocode(location)
if results:
lat = results[0]['geometry']['lat']
lng = results[0]['geometry']['lng']
print(f"📍 Latitude: {lat}, Longitude: {lng}")
# Create a map
my_map = folium.Map(location=[lat, lng], zoom_start=9)
folium.Marker([lat, lng], popup=location).add_to(my_map)
# Save to HTML
my_map.save("phone_location_map.html")
print("🗺️ Map saved as 'phone_location_map.html'")
else:
print("⚠️ Could not find location info.")
Output
Enter mobile number with country code: +12077233245 📍 Latitude: 45.6565049, Longitude: -68.7091349 🗺️ Map saved as 'phone_location_map.html'

The map is saved in HTML format in the working directory.
Summary
In this article, we learned how to find some basic information about a phone number using Python. We create several Python scripts to find the following:
- Validation of a phone number
- Location, carrier, and timezone information
- Format phone numbers
- Extract the phone number from a block of text
- Visualization of the location on a map
Now, check your own contact number information using one of the Python scripts above and see what you found.
Want more interesting Python topics? Check out this page, Creative Python. Here are a few examples to spark your interest:
- Access Your Mobile Camera as a Webcam Using a Python Script
- Steganography: Hide Your Text Behind an Image Using Python
- Create a Simple Brute-Force Password Cracker in Python
- Create a Network Traffic Monitor using Python
Happy Coding!



