Python Practice › Loop Programs › Program 68

Print keys and values

EasyLoop Programs · 68 of 90

Write a Python program to print the keys and values of a dictionary.

Advertisement
Input
{"name": "virat", "sports": "cricket"}
Output
name Virat
sports Cricket

Approach

  1. Loop over items() to get both at once.

Solution

Python
player = {"name": "virat", "sports": "cricket"}for key, value in player.items():    print(key, value.capitalize())
Output
name Virat
sports Cricket

Line by line

Line 2

items() yields (key, value) pairs that unpack into two variables.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs