Last modified: February 21, 2021

How to Get The Capital of any Country in Python

In this tutorial, we'll write a program that gets the capital city of any country in python by using CountryInfo.

Let's get started.

Install the CountryInfo library

Instaling CountryInfo via pip:

pip install countryinfo

Syntax


CountryInfo(country).capital()

Getting the capital of any country

In the code below, we'll understand how to get the capital of a specific county.


from countryinfo import CountryInfo

#country
country = input('Choose a country: ')
#capital
capital = CountryInfo(country).capital()
#print
print(capital)

output


Choose a country: france
Paris


Choose a country: morocco           
Rabat


Choose a country: united states
Washington D.C.