Skip to content
Advertisement

Print key value pairs from a dictionary

I want to pretty-print the key-value pairs of a dictionary. I have the following code:

dict = {'AAAAA': 2, 'BB': 1, 'CCCCCCCCC': 85}

for k, v in dict.items():
    print('{}........{}'.format(k, v))

This is what my code currently produces:

AAAAA........2
BB........1
CCCCCCCCC........85

But I want:

AAAAA............2
BB...............1
CCCCCCCCC........85

How do I make this happen?

Advertisement

Answer

Consider using len() to balance out the number of dots

dict = {'AAAAA': 2, 'BB': 1, 'CCCCCCCCC': 85}

total = 15
for k, v in dict.items():
    print(k, "." * (total - len(k)), v, sep="")
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement