Remember the days when you used a physical globe or scanned through a big bulky book to find the names of different regions and countries? With advancements in technology, especially the Internet, all this information is now available at the click of a button.
But what if you want to take it a step further and find essential data about a country such as its capital, geographic coordinates or currency? Look no further than Python’s amazing CountryInfo module, which can do the job in a matter of seconds.
What is countryinfo module?
Porimol Chandro developed the Country Info module. It has functions to fetch country’s capital city, geographic coordinates, timezone, area, population and much more.
You can get live weather data of any country or region and use it with information and enhance your dataset.
How to use the module’s functions
You can retrieve the information using the various functions of the CountryInfo module as follows.
1. Getting alternate names or spellings of a country
You can get alternate names or spellings of a country this way.
Start by importing the CountryInfo class from the CountryInfo module. You can then create an instance of CountryInfo by passing it a string containing the name of the country.
You can call various API methods on the CountryInfo object you create. For example, alt_spellings() returns a list of alternative country names.
2. Getting the capital and its geographic coordinates
You can use Capital() to get the capital of any country. Returns a string representing the name of the capital.
3. Getting the area of a country, its provinces and surrounding border countries
You can use the API method, area(), to get the area of any country in square kilometers.
You can get a list of provinces for a country by using provinces(). Each element in the list is a string representing a province.
You can get the border country names using the API method, borders(). Each element in the list is a country code in ISO-3 format, a three-character uppercase string.
4. Getting the calling code of a country
You can get the international calling code of a country by using calling_codes(). This returns a list of strings, each containing the numeric telephone prefix you need to call from the other country.
5. Getting currency, population and time zone
Get the official currency format of a country using the API method, currencies(). This method returns another list of strings.
6. Getting the region and subregion of a country
Get the generic region or continent that the country belongs to using the region() API method. Implement the code as follows.