-2

Hello I want to make text align so that it looks nicer. I want to print keys and corrsponding values from a dictionary.

for word in sorted(dict.keys()):
    print(f'{word}: {dict[word]}')

however this prints it like this

a:      4
c:  2
k:   444

I want it like this:

a:    4
c:    2
k:    444
Blue
  • 51
  • 4
  • Maybe this help: [Klick](https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data) – Mo7art Feb 14 '20 at 09:09
  • Does this answer your question? [Printing Lists as Tabular Data](https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data) – Nick Feb 22 '20 at 04:32

1 Answers1

0

Try this,

for key, value in sorted(dict.keys()):
    print(f'{key}: {value:>5}')

Reference Python: Format output string, right alignment

bumblebee
  • 1,811
  • 12
  • 19