# Capitals.py Copyright (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-05-24 File created. # 2022-12-16 Converted to Python 3. # This program demonstrates string methods upper(), lower(), # find(), strip(), and slicing operations. countries_and_capitals = \ "Finland Helsinki Usa Washington Denmark Copenhagen " \ "Afghanistan Kabul Russia Moscow England London " \ "Italy Rome France Paris Spain Madrid " \ "Portugal Lisbon Chile Santiago Japan Tokyo " \ "Sweden Stockholm Norway Oslo Pakistan Islamabad " \ "Iceland Reykjavik Hungary Budapest Holland Amsterdam " \ "Belgium Brussels Austria Vienna Israel Jerusalem " print( "\n This program may know the capital of a country." \ "\n Type in the name of some country:", end=' ' ) country_name = input().lower() country_name = country_name[ 0 ].upper() + country_name[ 1 : ] # The last two statements above could be replaced with the following # single statement: #country_name = raw_input().capitalize() index_of_country_name = countries_and_capitals.find( country_name ) if index_of_country_name != -1 : # The country name was found in countries_and_capitals text_after_country_name = countries_and_capitals[ index_of_country_name + len( country_name ) : ] # A space at the end of the string ensures that also # the last country-capital pair in the string works. text_after_country_name = text_after_country_name.strip() + " " capital_name = text_after_country_name[ 0 : text_after_country_name.find( " " ) ] print( "\n The capital of %s is %s." % ( country_name, capital_name ) )