在字符串后对齐文本以更加对称

Aligning text to be more symmetrical after a string

你好,我想让文本对齐,这样看起来更漂亮。我想打印字典中的键和相应的值。

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

但是它打印成这样

a:      4
c:  2
k:   444

我想要这样:

a:    4
c:    2
k:    444

试试这个,

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

引用Python: Format output string, right alignment