美文网首页
python 中字符串对齐处理

python 中字符串对齐处理

作者: 北冢 | 来源:发表于2017-12-11 21:25 被阅读0次

使用 str.ljust 、 str.rjust 和 str.just 方法

d = {
    "name": "jessun",
    "age": "18",
    "gender": "male",
    "height": "172"
}

w = max(map(len, d.keys()))
for k in d:
    print(k.ljust(w),':',d[k])
# 输出为:
# name   : jessun
# age    : 18
# gender : male
# height : 172

相关文章

网友评论

      本文标题:python 中字符串对齐处理

      本文链接:https://www.haomeiwen.com/subject/whmwixtx.html