阅读背景:

如何批量对字符串进行左,右,中对齐

来源:互联网 
# # 如何对字符串进行左,右,中对齐
#
# 某字典存储了一系列属性值,
# {
#     'lodDist':100.0,
#     'SmallCull':100.0,
#     'DistCull':100.0,
#     'Trilinear':100.0,
#     'farclip':100.0,
# }
# 在程序中,我们想要以以下工整的格式将其内容输出,如何处理?
# 'lodDist'    : 100.0,
# 'SmallCull'  : 100.0,
# 'DistCull'   : 100.0,
# 'Trilinear'  : 100.0,
# 'farclip'    : 100.0,



import os, stat
import re

def main():
    s='abc'
    s=s.ljust(20)
    # "abc                 "
    s = 'abc'
    s = s.ljust(20,"=")
    # "abc================="
    s = 'abc'
    s = s.rjust(20,"=")
    # "=================abc"
    s = 'abc'
    s = s.center(20,"=")
    # "========abc========="
    print(s)
    pass
def main2():
    d={
        'lodDist':100.0,
        'SmallCull':100.0,
        'DistCull':100.0,
        'Trilinear':100.0,
        'farclip':100.0,
    }
    # 左右居中对齐
    s1=format('abc','<20')
    s2=format('abc','>20')
    s3=format('abc','^20')
    print(s1)
    print(s2)
    print(s3)

    maxNumber=max(map(len,d.keys()))
    for x in d:
        print(x.ljust(maxNumber),":",d[x])
    pass

main2()
# # 如何对字符串进行左,右,中对齐
#
# 某字典存储了一系列属性值,
# {
#    



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: