Total length of string keys

EasyDictionary Programs · 76 of 94

Write a Python program to count the number of keys and the total characters in them.

Advertisement
Input
{"virat": 50, "Rohit": 40, "Rahul": 25}
Output
Keys: 3
Total length of keys: 15

Approach

  1. len(d) for keys, sum(len(k) ...) for characters.

Solution

Python
d = {"virat": 50, "Rohit": 40, "Rahul": 25}print("Keys:", len(d))print("Total length of keys:", sum(len(k) for k in d))
Output
Keys: 3
Total length of keys: 15
Advertisement

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs