How to Get Country Information using Python

In this tutorial, we will learn about how we can get country information using the Python program. We will discuss a Python module to obtain information about capital, currencies, official language, and many other things. We will also learn to get the country from phone numbers. Let's start the tutorial.

Python countryinfo Module

Python provides the countryinfo module to get the information or properties about any country including the ISO information, states, and languages are spoken, currencies and various other details. This module doesn't come with the Python pre-install version. To use it, we need to install it using the pip command. The country module is a user-friendly and open-source module. Let's install the countryinfo module in the terminal.

This command will install the module in the local system.

Now let's move to the coding part.

Code Implementation

Let's understand the following steps -

  1. First, we import the CountryInfo class from countryinfo module and use the input() function to get the country name as an input.
  2. We create a class name CountryDetails that takes a country name as input and stores it in a variable.
  3. To use the CountryDetails class, we provide a string containing a country's name as an argument. This will create an instance of the `CountryDetails` class that represents the specified country.
  4. Utilize multiple methods within the `CountryDetails` class to manipulate the object and gather specific information about the country. For example, these methods might retrieve details such as population, capital city, currency, and languages spoken in that country.
  5. Access the necessary information using the dot notation, which allows you to retrieve the specific details from the `CountryInfo` object.
  6. After obtaining the country's details, print each specific information individually, like printing the country name, capital city, currency, and languages spoken in that country.

Let's implement the above steps in the code.

Example -

Output - 1: For India

Enter your country: India
Capital is: New Delhi
Currency is: ['INR']
Languages: ['hi', 'en']
Borders are: ['AFG', 'BGD', 'BTN', 'MMR', 'CHN', 'NPL', 'PAK', 'LKA']
Provinces are: ['Andaman and Nicobar Islands', 'Andhra Pradesh', 'Arunachal Pradesh', 'Assam', 'Bihar', 'Chandigarh', 'Chhattisgarh', 
'Dadra and Nagar Haveli', 'Daman and Diu', 'Delhi', 'Goa', 'Gujarat', 'Haryana', 'Himachal Pradesh', 'Jammu and Kashmir', 'Jharkhand', 'Karnataka', 'Kerala', 'Lakshadweep', 'Madhya Pradesh', 'Maharashtra', 'Manipur', 'Meghalaya', 'Mizoram', 'Nagaland', 'Odisha', 'Puducherry', 'Punjab', 'Rajasthan', 'Sikkim', 'Tamil Nadu', 'Telangana', 'Tripura', 'Uttar Pradesh', 'Uttarakhand', 'West Bengal']        
Area is: 3287590
Calling codes: ['91']
Capital Latitudes and Longitudes are: [28.614179, 77.202266]
TimeZone: ['UTC+05:30']
Population: 1263930000
Other names: ['IN', 'Bh?rat', 'Republic of India', 'Bharat Ganrajya']

Output - 2: For USA

Enter your country: USA
Capital is: Washington D.C.
Currency is: ['USD', 'USN', 'USS']
Languages: ['en']
Borders are: ['CAN', 'MEX']
Provinces are: ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'District of Columbia', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']
Area is: 9629091
Calling codes: ['1']
Capital Latitudes and Longitudes are: [38.894986, -77.036571]
TimeZone: ['UTC?12:00', 'UTC?11:00', 'UTC?10:00', 'UTC?09:00', 'UTC?08:00', 'UTC?07:00', 'UTC?06:00', 'UTC?05:00', 'UTC?04:00', 'UTC+10:00', 'UTC+12:00']
Population: 319259000
Other names: ['US', 'USA', 'United States of America']

Output - 3: For Russia

Enter your country: Russia
Capital is: Moscow
Currency is: ['RUB']
Languages: ['ru']
Borders are: ['AZE', 'BLR', 'CHN', 'EST', 'FIN', 'GEO', 'KAZ', 'PRK', 'LVA', 'LTU', 'MNG', 'NOR', 'POL', 'UKR']
Provinces are: ['Adygeya (Maykop)', 'Aginskiy Buryatskiy (Aginskoye)', 'Altay (Gorno-Altaysk)', 'Altayskiy (Barnaul)', 'Amurskaya (Blagoveshchensk)', "Arkhangel'skaya", 'Astrakhanskaya', 'Bashkortostan (Ufa)', 'Belgorodskaya', 'Bryanskaya', 'Buryatiya (Ulan-Ude)', 'Chechnya (Groznyy)', 'Chelyabinskaya', 'Chitinskaya', "Chukotskiy (Anadyr')", 'Chuvashiya (Cheboksary)', 'Dagestan (Makhachkala)', 'Evenkiyskiy (Tura)', "Ingushetiya (Nazran')", 'Irkutskaya', 'Ivanovskaya', "Kabardino-Balkariya (Nal'chik)", 'Kaliningradskaya', 'Kalmykiya (Elista)', 'Kaluzhskaya', 'Kamchatskaya (Petropavlovsk-Kamchatskiy)', 'Karachayevo-Cherkesiya (Cherkessk)', 'Kareliya (Petrozavodsk)', 'Kemerovskaya', 'Khabarovskiy', 'Khakasiya (Abakan)', 'Khanty-Mansiyskiy (Khanty-Mansiysk)', 'Kirovskaya', 'Komi (Syktyvkar)', 'Komi-Permyatskiy (Kudymkar)', 'Koryakskiy (Palana)', 'Kostromskaya', 'Krasnodarskiy', 'Krasnoyarskiy', 'Kurganskaya', 'Kurskaya', 'Leningradskaya', 'Lipetskaya', 'Magadanskaya', 'Mariy-El (Yoshkar-Ola)', 'Mordoviya (Saransk)', 'Moskovskaya', 'Moskva (Moscow)', 'Murmanskaya', "Nenetskiy (Nar'yan-Mar)", 'Nizhegorodskaya', 'Novgorodskaya', 'Novosibirskaya', 'Omskaya', 'Orenburgskaya', 'Orlovskaya (Orel)', 'Penzenskaya', 'Permskaya', 'Primorskiy (Vladivostok)', 'Pskovskaya', 'Rostovskaya', 'Ryazanskaya', 'Sakha (Yakutsk)', 'Sakhalinskaya (Yuzhno-Sakhalinsk)', 'Samarskaya', 'Sankt-Peterburg (Saint Petersburg)', 'Saratovskaya', 'Severnaya Osetiya-Alaniya [North Ossetia] 
(Vladikavkaz)', 'Smolenskaya', "Stavropol'skiy", 'Sverdlovskaya (Yekaterinburg)', 'Tambovskaya', "Tatarstan (Kazan')", 'Taymyrskiy (Dudinka)', 'Tomskaya', "Tul'skaya", 'Tverskaya', 'Tyumenskaya', 'Tyva (Kyzyl)', 'Udmurtiya (Izhevsk)', "Ul'yanovskaya", "Ust'-Ordynskiy 
Buryatskiy (Ust'-Ordynskiy)", 'Vladimirskaya', 'Volgogradskaya', 'Vologodskaya', 'Voronezhskaya', 'Yamalo-Nenetskiy (Salekhard)', 'Yaroslavskaya', 'Yevreyskaya']
Area is: 17124442
Calling codes: ['7']
Capital Latitudes and Longitudes are: [55.750446, 37.617494]
TimeZone: ['UTC+03:00', 'UTC+04:00', 'UTC+06:00', 'UTC+07:00', 'UTC+08:00', 'UTC+09:00', 'UTC+10:00', 'UTC+11:00', 'UTC+12:00']
Population: 146233000
Other names: ['RU', 'Rossiya', 'Russian Federation', '?????????? ?????????', 'Rossiyskaya Federatsiya']

Output - 4: For Iceland

Enter your country: Iceland
Capital is: Reykjavik
Currency is: ['ISK']
Languages: ['is']
Borders are: []
Provinces are: ['Akranes', 'Akureyri', 'Arnessysla', 'Austur-Bardhastrandarsysla', 'Austur-Hunavatnssysla', 'Austur-Skaftafellssysla', 'Borgarfjardharsysla', 'Dalasysla', 'Eyjafjardharsysla', 'Gullbringusysla', 'Hafnarfjordhur', 'Husavik', 'Isafjordhur', 'Keflavik', 'Kjosarsysla', 'Kopavogur', 'Myrasysla', 'Neskaupstadhur', 'Nordhur-Isafjardharsysla', 'Nordhur-Mulasys-la', 'Nordhur-Thingeyjarsysla', 'Olafsfjordhur', 'Rangarvallasysla', 'Reykjavik', 'Saudharkrokur', 'Seydhisfjordhur', 'Siglufjordhur', 'Skagafjardharsysla', 'Snaefellsnes-og Hnappadalssysla', 'Strandasysla', 'Sudhur-Mulasysla', 'Sudhur-Thingeyjarsysla', 'Vesttmannaeyjar', 'Vestur-Bardhastrandarsysla', 'Vestur-Hunavatnssysla', 'Vestur-Isafjardharsysla', 'Vestur-Skaftafellssysla']
Area is: 103000
Calling codes: ['354']
Capital Latitudes and Longitudes are: [64.145981, -21.942237]
TimeZone: ['UTC']
Population: 328170
Other names: ['IS', 'Island', 'Republic of Iceland', 'L��veldi� �sland']

We have got the information of the multiple cities. This module is quite helpful in order to find the all-related information related to a specific country and use according to a requirements.