【问题标题】:Aligning text to be more symmetrical after a string在字符串之后对齐文本以更加对称
【发布时间】:2020-02-14 09:07:28
【问题描述】:

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

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

但是这样打印出来的

a:      4
c:  2
k:   444

我想要这样:

a:    4
c:    2
k:    444

【问题讨论】:

标签: python format f-string


【解决方案1】:

试试这个,

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

参考Python: Format output string, right alignment

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-30
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多