0

I was reading http://www.python-course.eu/python3_formatted_output.php where I found this python code.

capital_country = {"United States" : "Washington", 
                   "US" : "Washington", 
                   "Canada" : "Ottawa",
                   "Germany": "Berlin",
                   "France" : "Paris",
                   "England" : "London",
                   "UK" : "London",
                   "Switzerland" : "Bern",
                   "Austria" : "Vienna",
                   "Netherlands" : "Amsterdam"}

print("Countries and their capitals:")
for c in capital_country:
    format_string = c + ": {" + c + "}" 
    print(format_string.format(**capital_country))

I know capital_country is what is called 'dictionary' in Python. But I cannot understand the last two lines. Of course I understand the for loop iterates through the dictionary elements. Could somebody explain it to me?

Chan Kim
  • 5,177
  • 12
  • 57
  • 112
  • 1
    Very bad idea, how to use `format`: just write `for country, capital in capital_country.items(): print("{0}: {1}".format(country, capital))` – Daniel May 23 '16 at 05:57
  • There's more than just ** (doublestar) keyword arguments going on here -- it's also a bad programming design (particularly for a Python course) of which I had a detailed explanation before folks went and closed the question. Sigh. – cdlane May 23 '16 at 06:10

0 Answers0