Skip to main content

countrystatecity-postal-codes

PyPI Python Version License Type Checked postal-codes postal-codes

Official Python package for postal/ZIP code data — 844,000+ postcodes with localities and coordinates across 125 countries, plus format/validation regex for all 250+ countries. Part of the countrystatecity ecosystem.

Like the countries package, postcode data is lazy-loaded per country — installing the package doesn't load anything into memory until you ask for a specific country's data.

Installation

pip install countrystatecity-postal-codes

Quick Start

from countrystatecity_postal_codes import (
    get_countries_with_postal_data,
    get_postal_info_by_country,
    get_postcodes_of_country,
    get_postcode_by_code,
    search_postcodes,
    validate_postcode,
)

# Postal code format/regex for a country
us_info = get_postal_info_by_country("US")
# CountryPostalInfo(countryCode="US", postalCodeFormat="#####", postalCodeRegex="^\\d{5}$", ...)

# Validate a postcode against the country's known format
validate_postcode("US", "10001")   # True
validate_postcode("US", "abcde")   # False

# All postcodes for a country (lazy loaded)
postcodes = get_postcodes_of_country("AD")
# [Postcode(code="AD100", localityName="Canillo", ...), ...]

# Look up a specific postcode
pc = get_postcode_by_code("AD", "AD100")
print(pc.localityName)  # Canillo

# Search by code or locality name within a country
results = search_postcodes("AD", "canillo")

# List postal-format metadata for every country
all_countries = get_countries_with_postal_data()

Data Model

class CountryPostalInfo(BaseModel):
    countryCode: str               # ISO2 country code (e.g., "US")
    countryName: str                # Country name (e.g., "United States")
    postalCodeFormat: Optional[str] # Format pattern (e.g., "#####")
    postalCodeRegex: Optional[str]  # Validation regex (e.g., "^\\d{5}$")
    postcodeCount: int              # Number of individual postcodes available for this country

class Postcode(BaseModel):
    code: str                       # The postal code value (e.g., "10001")
    countryCode: str                # ISO2 country code (e.g., "US")
    stateCode: Optional[str]        # State/province code, if known
    localityName: Optional[str]     # Human-readable place name
    type: Optional[str]             # Granularity: full | outward | sector | district | area
    latitude: Optional[float]
    longitude: Optional[float]

API Reference

Function Description
get_countries_with_postal_data() Get postal format/regex metadata for all countries
get_postal_info_by_country(code) Get postal format/regex metadata for an ISO2 country code
get_postcodes_of_country(code) Get all postcodes for a country (lazy loaded)
get_postcode_by_code(code, postcode) Look up a specific postcode within a country
search_postcodes(code, query) Search postcodes within a country by code or locality name
validate_postcode(code, postcode) Validate a postcode against the country's known format

Coverage

Individual postcode listings are available for 125 countries with source data upstream (844,248 postcodes total, varying granularity per country). get_postal_info_by_country() returns format/regex metadata for all 250+ countries where known, even if per-postcode listings aren't available — check postcodeCount to see how many individual postcodes are available for a given country.

License

ODbL-1.0 — see LICENSE.

Other Packages in this Ecosystem

Package Description
countrystatecity-countries 250+ countries, 5,000+ states, 150,000+ cities
countrystatecity-timezones 400+ IANA timezones with country associations and time conversion
countrystatecity-currencies Currency codes, names, and symbols
countrystatecity-translations Country name translations in 18+ languages
countrystatecity-phonecodes International phone/dialing codes for 250+ countries
countrystatecity-regions Continents and geographic subregions for 250+ countries

Data sourced from countries-states-cities-database.


Made with ❤️ by dr5hn

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

countrystatecity_postal_codes-1.0.0.tar.gz (6.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file countrystatecity_postal_codes-1.0.0.tar.gz.

File metadata

File hashes

Hashes for countrystatecity_postal_codes-1.0.0.tar.gz
Algorithm Hash digest
SHA256 72ec2daca216a977f71fe374e67d137aea2935321ad8065797cef70ce0881776
MD5 15b1ddbcf58fb261fa3dbefa8c8e18fa
BLAKE2b-256 821e1b6b37fba2146eeb60212d20f810004481db2bc1cafead51e0112659b333

See more details on using hashes here.

File details

Details for the file countrystatecity_postal_codes-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for countrystatecity_postal_codes-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8010509ee4bc68c1cef4eab0cc28d050d76b4900ef07c59090e7dfadc27d7a5a
MD5 af1cee1a8fce0a14455a3a07933eb99d
BLAKE2b-256 8147a2f33587e89ccb7425b66b72d63f07bb968bc61566f8e54ebb33764f507a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page