gitmyhub

python-phonenumbers

Python ★ 3.8k updated 13d ago

Python port of Google's libphonenumber

A Python library for parsing, validating, and formatting phone numbers from any country, based on Google's libphonenumber. Supports E.164 format, carrier lookup, geographic region detection, and text scanning.

Pythonpipsetup: easycomplexity 2/5

This is a Python library for working with phone numbers from anywhere in the world. It is a port, meaning a faithful translation into Python, of Google's libphonenumber, which is the same system Google uses internally. The library knows the rules for phone number formats in every country: how many digits they have, what prefixes they use, which number ranges are actually assigned to real carriers, and how numbers should be displayed.

The core workflow is: take a phone number as a string, tell the library what country it belongs to (or let it figure that out from an international prefix), and get back a structured object you can work with. From there you can check whether the number is plausible, whether it is actually valid in the real world, and format it in different ways. Common formats include the local national format used within a country, the international format with the country code prefix, and the E.164 format which is a standardized compact form used in databases and APIs.

Beyond parsing and formatting, the library supports several related tasks. It can scan a block of text and find all phone numbers hidden inside a sentence or paragraph. It can report the geographic region associated with a number, like a city or country, in multiple languages. For mobile numbers in countries where this data is available, it can identify the carrier that originally issued the number. It can also return the time zone or time zones that correspond to a given number.

Memory is handled thoughtfully: the base metadata loads only the country data it actually needs when your code first uses a particular region, rather than loading everything at once. Geographic, carrier, and timezone data are in separate optional packages so you include only what your project actually uses. Installation is a single pip command.

Where it fits