Last modified: Jan 10, 2023 By Alexander Williams
Python-pycountry | Understand How to Use pycountry
pycountry is a python library that provides the ISO databases for the standards:
- Languages
- Countries
- Deleted countries
- Subdivisions of countries
- Currencies
- Scripts
In this article, we'll learn the most pycountry's methods with examples.
pycountry Installation
To install pycountry via pip, run the command below:
pip install pycountry
How to use pycountry
Usage of the pycountry library is very simple. Let's see how to use it.
pycountry.countries
The pycountry.countries method returns the information of countries like:
- alpha_2
- alpha_3
- flag
- name
- numeric
Use pycountry.countries in example:
import pycountry # 👉️ Import module
countries = pycountry.countries # 👉️ Get All World Countries info
for country in countries:
print(country)
The output:
Country(alpha_2='BL', alpha_3='BLM', flag='🇧🇱', name='Saint Barthélemy', numeric='652')
Country(alpha_2='BY', alpha_3='BLR', flag='🇧🇾', name='Belarus', numeric='112', official_name='Republic of Belarus')
Country(alpha_2='BZ', alpha_3='BLZ', flag='🇧🇿', name='Belize', numeric='084')
Country(alpha_2='BM', alpha_3='BMU', flag='🇧🇲', name='Bermuda', numeric='060')
Country(alpha_2='BO', alpha_3='BOL', common_name='Bolivia', flag='🇧🇴', name='Bolivia, Plurinational State of', numeric='068', official_name='Plurinational State of Bolivia')
Country(alpha_2='BR', alpha_3='BRA', flag='🇧🇷', name='Brazil', numeric='076', official_name='Federative Republic of Brazil')
Country(alpha_2='BB', alpha_3='BRB', flag='🇧🇧', name='Barbados', numeric='052')
Country(alpha_2='BN', alpha_3='BRN', flag='🇧🇳', name='Brunei Darussalam', numeric='096')
Country(alpha_2='BT', alpha_3='BTN', flag='🇧🇹', name='Bhutan', numeric='064', official_name='Kingdom of Bhutan')
Country(alpha_2='BV', alpha_3='BVT', flag='🇧🇻', name='Bouvet Island', numeric='074')
Country(alpha_2='BW', alpha_3='BWA', flag='🇧🇼', name='Botswana', numeric='072', official_name='Republic of Botswana')
Country(alpha_2='CF', alpha_3='CAF', flag='🇨🇫', name='Central African Republic', numeric='140')
If you want to get specific info, follow this code:
for country in countries:
print("Name:", country.name) # 👉️ Name of Country
print("Flag", country.flag) # 👉️ Flag of Country
print("Code", country.alpha_3) # 👉️ Code of Country
print("*" * 5)
Output:
Name: Aruba
Flag 🇦🇼
Code ABW
*****
Name: Afghanistan
Flag 🇦🇫
Code AFG
*****
Name: Angola
Flag 🇦🇴
Code AGO
*****
Name: Anguilla
Flag 🇦🇮
Code AIA
*****
Name: Åland Islands
Flag 🇦🇽
Code ALA
*****
Name: Albania
Flag 🇦🇱
Code ALB
You can also use pycountry.countries method to get the number of countries in the world.
countries = pycountry.countries # 👉️ Get All World Countries info
countries_n = len(countries) # 👉️ Get number of countries
print("Total:", countries_n)
Output:
Total: 249
With pycountry.countries.get we can get information on a specific country by targeting:
- alpha_2
- alpha_3
- name
In the following example, get United States Information by using alpha_2:
usa = pycountry.countries.get(alpha_2='US') # 👉️ Get United States Info by alpha_2
print(usa)
Output:
Country(alpha_2='US', alpha_3='USA', flag='🇺🇸', name='United States', numeric='840', official_name='United States of America')
In the following example, get United States Information using the name:
usa = pycountry.countries.get(name='United States') # 👉️ Get United States Info by name
pycountry.scripts
the pycountry.scripts method returns the information of the languages. Let's see how to use it.
languages = pycountry.scripts # 👉️ Get languages info using scripts
# 👇 Print one by one
for lan in languages:
print(lan)
Output:
Script(alpha_4='Adlm', name='Adlam', numeric='166')
Script(alpha_4='Afak', name='Afaka', numeric='439')
Script(alpha_4='Aghb', name='Caucasian Albanian', numeric='239')
Script(alpha_4='Ahom', name='Ahom, Tai Ahom', numeric='338')
Script(alpha_4='Arab', name='Arabic', numeric='160')
Script(alpha_4='Aran', name='Arabic (Nastaliq variant)', numeric='161')
pycountry.currencies
pycountry.currencies method returns countries' currency information. You can see how to use it In the following example.
currencies = pycountry.currencies # 👉️ Get countries Currencies
# 👇 Print one by one
for currency in currencies:
print(currency)
The output of the first three results:
Currency(alpha_3='JOD', name='Jordanian Dinar', numeric='400')
Currency(alpha_3='JPY', name='Yen', numeric='392')
Currency(alpha_3='KES', name='Kenyan Shilling', numeric='404')
...
pycountry.languages
The pycountry.languages method is similar to pycountry.scripts but provides different language information. Here is a simple example.
w_languages = pycountry.languages # 👉️ Get countries languages
# 👇 Print one by one
for l in w_languages:
print(l)
Output:
Language(alpha_3='zua', name='Zeem', scope='I', type='L')
Language(alpha_3='zuh', name='Tokano', scope='I', type='L')
Language(alpha_2='zu', alpha_3='zul', name='Zulu', scope='I', type='L')
Language(alpha_3='zum', name='Kumzari', scope='I', type='L')
Language(alpha_3='zun', name='Zuni', scope='I', type='L')
Language(alpha_3='zuy', name='Zumaya', scope='I', type='L')
..
Now let's get France's language.
france_language = pycountry.languages.get(alpha_2='FR') # 👉️ Get langaue of France
print(france_language)
Output:
Language(alpha_2='fr', alpha_3='fra', bibliographic='fre', name='French', scope='I', type='L')
Print the name of the language:
print(france_language.name) # 👉️ Print name of language
Output:
French
Conclusion
In this tutorial, we've learned How to use the Python-pycountry library with examples. Visit extract country from text to see how to use pycountry to extract the country from a text.
I hope this article helps you.