How to validate a phone number in Python
You can validate country codes of phone numbers with Regex. However, it is not an efficient solution. You don't want to create a custom Regex for each of the countries. It will make the process very complex. Fortunately, there is a library that helps you to validate phone numbers in Python easily. It is known as Python Phonenumbers. In this post, you will find the details of using this Python phone number validator to verify phone numbers. Also, you will find the process of getting user-specific geolocation data. If you want to dive into other Python guides, make sure to read about geolocation an IP address in Python or how to validate emails using Python.
Validate with Python PhoneNumbers
You can validate phone numbers with the Python library Phonenumbers using two different methods. Let's take a look at how we can use the Phone numbers solution.
Validate Phone Number Using the is_possible_number() Method:
1. First, you have to import the phonenumbers library.
import phonenumbers
2. Create a variable, called string_phone_number. It will contain your ten digit number.
string_phone_number = "+40032215789"
3. Then you need to parse the given phone number.
phone_number = phonenumbers.parse(string_phone_number)
4. Finally, you have to use the is_possible_number() method to perform the validation. Then you can set up the print output.
print(phonenumbers.is_possible_number(phone_number))
Overall, the code will look like this:
import phonenumbers
my_string_number = "+40021234567"
my_number = phonenumbers.parse(my_string_number)
print(phonenumbers.is_possible_number(my_number))
You will get this output:
True
Validate Phone Number Using the is_valid_number() Method:
1. Import the phonenumbers library.
import phonenumbers
2. Create the string_phone_number variable. It will contain your phone number.
string_phone_number = "+40032215789"
3. Then you have to parse the string_phone_number.
phone_number = phonenumbers.parse(string_phone_number)
4. Now, you can utilize the is_valid_number() method to start validating phone number
print(phonenumbers.is_valid_number(my_number))
Overall, the code will look like this with the phonenumber object:
import phonenumbers
my_string_number = "+40021234567"
my_number = phonenumbers.parse(my_string_number)
print(phonenumbers.is_valid_number(my_number))
You will get this output:
True
The Problem with the Two Methods:
As you can see, the only challenge with the above code is the two different results by using the same number. The is_possible_number() method provides False output. But the is_valid_number() method gives you True. What's the reason behind the difference?
is_possible_number() method verifies the phone number by checking the length of the parsed number. On the other hand, is_valid_number() method looks for the length, phone number prefix, and region to perform a complete validation.
As is_valid_number() method performs full validation, it is a bit slower in delivering the result. So, while iterating over a large list of phone numbers, you can consider using the is_possible_number() method. It will provide results faster. But as you can see in the code examples, the results may not always be right. There is a limitation with the accuracy.
How can you fix the issue? You can try using an API, like Abstract's Phone Validation API, to validate phone numbers without worrying about the accuracy. You will find the details later in this post.
Getting location from Phone numbers in Python
A phone number is loaded with a variety of information, including user-specific geolocation data. You can use the data to send promotional notifications at the right time in terms of the customer's time zone. You don't want to notify the people in the middle of the night. Just pay close attention to formatting phone numbers.
How to Extract Geolocation from the Phone Number in Python
1. First, you have to import phonenumbers library and timezone.
import phonenumbers
from phonenumbers import timezone
2. Then you have to parse your phone number.
phone_number = phonenumbers.parse("+447986123456")
3. Now, you can print the result.
print(timezone.time_zones_for_number(phone_number))
Overall, the code will look like this:
import phonenumbers
from phonenumbers import timezone
phone_number = phonenumbers.parse("+447986123456")
print(timezone.time_zones_for_number(phone_number))
You will get this output:
('Europe/Guernsey', 'Europe/Isle_of_Man', 'Europe/Jersey', 'Europe/London')
How to Extract Carrier Name from the Phone Number in Python
You might want to group your phone numbers by their carrier. It will have an impact on your product cost. The Phonenumbers library deals with very easy to extract the name of the carrier from a valid mobile number. You just need to follow these steps:
1. Import phonenumbers library and timezone.
import phonenumbers
from phonenumbers import timezone
2. Now, you have to parse the phone number.
phone_number = phonenumbers.parse("+40721234567")
3. Finally, you have to use the carrier.name_for_number() method to get the carrier's name of the given phone number.
print(carrier.name_for_number(phone_number, "en"))
Overall, the code will look like this:
import phonenumbers
from phonenumbers import carrier
phone_number = phonenumbers.parse("+40721234567")
print(carrier.name_for_number(phone_number, "en"))
You will get this output:
Vodafone
The Limitation of Extracting Carrier Names with Phonenumbers library
As you can see, the Phonenumbers library has made it very easy for you to extract the name of the carrier. However, there is an issue. In most cases, the Phonenumbers library will be able to provide the name of the carrier accurately for international numbers. However, the result may not be 100% accurate all the time.
Using an API to validate phone numbers
APIs can make the process of validating phone numbers very simple. They enable you to get the job done with just a few lines of code. Also, they help you to extract important data, including carrier, country name, and timezone, with greater accuracy.
You can consider using Abstract's Phone Validation API. It is lightweight and super-fast. It can help you to determine the validity of phone numbers in 190 countries. It's very easy to use. You just need to submit the API key and phone number. The Phone Validation API will perform the verification instantly. Also, it provides you with additional data with greater accuracy, including carrier name, line type, city, country, and region.
Let's try verifying this phone number with Abstract's Phone Validation API: 14154582468.
https://phonevalidation.abstractapi.com/v1/
? api_key = YOUR_UNIQUE_API_KEY
& phone = 14154582468
If the request is successful, you will get this response in JSON format:
{
"phone": "14154582468",
"valid": true,
"local_format":"4154582468",
"international_format": "+14154582468",
"country_name": "United States of America",
"country_code": "US",
"country_prefix":"+1",
"registered_location": "San Francisco, CA",
"carrier":"Verizon USA",
"line_type": "Mobile",
}
Notice that "valid" is set to true, meaning that we entered one of the valid mobile numbers. That means the given phone number is valid. Also, you get a variety of useful data, like country name, registered location, carrier, etc. Just make sure to format phone numbers correctly.
As you can see, the Abstract's Phone Validation API has made the verification process very simple. It doesn't require you to write any code from scratch. You just need to get the API key and make a request for verifying the phone number. There is no complexity. You will get the phone number validated instantly.
Wrapping Up
Now, you have learned different methods for validating phone numbers. You can use the Python Phonenumbers library. However, the easiest way is using Abstract's Phone Validation API. It can help you perform the verification quickly and effortlessly.
Frequently Asked Questions
What is the Python phonenumbers library and why use it for validation?
The phonenumbers library is a Python port of Google's libphonenumber and contains numbering-plan metadata for every country. It lets you parse and validate phone numbers without making an external request. For most projects it is the simplest starting point because it handles international formats out of the box.
What is the difference between is_valid_number() and is_possible_number()?
is_possible_number() checks only the length of the parsed number, so it runs faster but can return false positives. is_valid_number() also checks the prefix and region data, making it a complete validation. For most use cases where accuracy matters, is_valid_number() is the right choice.
Do I need to parse a phone number before validating it?
Yes. The phonenumbers library requires you to call phonenumbers.parse() on the raw string first to produce a PhoneNumber object. You then pass that object to is_valid_number() or is_possible_number(). Skipping the parse step will raise a TypeError.
Can I get carrier or location data alongside validation using Python alone?
The phonenumbers library exposes helpers for carrier name, timezone, and geographic location through methods like carrier.name_for_number() and timezone.time_zones_for_number(). However, carrier lookups from the library may not be fully accurate, especially for international numbers, because the underlying data is not always current.
When should I use an API like Abstract's Phone Validation API instead of the local library?
If you need reliable, up-to-date carrier, line type (mobile vs. landline), and registered location data across 190 countries, a dedicated API is more dependable than the local library. The local library is a good fit for quick format checks; an API is better when downstream logic (such as SMS routing or fraud screening) depends on accurate carrier or line-type information.
Should phone numbers be in E.164 format before validation?
Providing numbers in E.164 format (e.g. +40021234567) is the most reliable approach because the leading + and country code remove ambiguity during parsing. If you receive numbers without a country code, you can pass a default region hint to phonenumbers.parse() to help the library interpret the number correctly.


