pycountry-convert: Country and Continent Code Conversion

0.7.2 · maintenance · verified Tue Apr 14

pycountry-convert is a Python library that extends `pycountry` by providing conversion functions between ISO country names, various country codes (Alpha-2, Alpha-3), and continent names. It facilitates mapping between these geographical identifiers. The current version is 0.7.2, released in 2018, indicating an infrequent release cadence.

Warnings

Install

Imports

Quickstart

This example demonstrates how to convert a country name to its ISO Alpha-2 code, then to its continent code, and finally to the continent name, chaining the necessary functions. Direct conversion from country name to continent name is not available, requiring these intermediate steps.

import pycountry_convert as pc

# Convert country name to Alpha-2 code
country_name = 'Germany'
try:
    country_alpha2 = pc.country_name_to_country_alpha2(country_name, cn_name_format='default')
    print(f"Alpha-2 code for {country_name}: {country_alpha2}")

    # Convert Alpha-2 code to continent code
    continent_code = pc.country_alpha2_to_continent_code(country_alpha2)
    print(f"Continent code for {country_alpha2}: {continent_code}")

    # Convert continent code to continent name
    continent_name = pc.convert_continent_code_to_continent_name(continent_code)
    print(f"Continent name for {continent_code}: {continent_name}")

except KeyError:
    print(f"Could not find data for country: {country_name}")

view raw JSON →