如何对字符串进行左右居中的对齐?

需求:
某个字典存储了一系列属性值,
{
'lodDist':100.0,
'SmallCull':0.04,
'DistCull':500.0,
'trilinear':40
'farclip':477
}
在程序中,我们想以以下工整格式将其内容输出,如何处理?
SmallCull:0.04
farclip :477
lodDist :100.0
Distcull :500.0
trilinear:40

思路:
1、使用字符串的str.ljust(),str.rjust(),str.center()进行左,右,居中对齐
2、使用format()方法,传递类似'<20','>20','^20'参数完成同样任务

代码:

d = {
   'lodDist':100.0,
   'SmallCull':0.04,
   'DistCull':500.0,
   'trilinear':40,
   'farclip':477
}

#找出字典中key中长度最长的key
a = max(map(len,d.keys()))
print(type(a))

# 方法一:
for x in d:
    print(x.ljust(a),':',d[x])

# 方法二:
# 将int类型的a,转化成字符串,注意'<'+b格式的书写
b = str(a)
for i in d:
    print(format(i,'<'+b),':',d[i])

=============================================================================================

>>> s = 'abc'

>>> s.ljust(10)
'abc       '

>>> len(s.ljust(10))
10

>>> s.ljust(2)
'abc'

>>> len(s.ljust(10),'*')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-40-33ba6f9bcaca> in <module>
----> 1 len(s.ljust(10),'*')

TypeError: len() takes exactly one argument (2 given)

>>> 

>>> 

>>> 

>>> s.ljust(10,'*')
'abc*******'

>>> s.ljust(10,'^')
'abc^^^^^^^'

>>> s.ljust(10,'^'*)
  File "<ipython-input-43-2d2b1ad48ba0>", line 1
    s.ljust(10,'^'*)
                   ^
SyntaxError: invalid syntax


>>> s.ljust(10,'^*')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-44-81973743ac83> in <module>
----> 1 s.ljust(10,'^*')

TypeError: The fill character must be exactly one character long

>>> s.rjust(10,'*')
'*******abc'

>>> s.center(10,'*')
'***abc****'

>>> s.center(11,'*')
'****abc****'

>>> format?
Signature: format(value, format_spec='', /)
Docstring:
Return value.__format__(format_spec)

format_spec defaults to the empty string.
See the Format Specification Mini-Language section of help('FORMATTING') for
details.
Type:      builtin_function_or_method

>>> format(s,'<10')
'abc       '

>>> format(s,'>10')
'       abc'

>>> format(s,'^10')
'   abc    '

>>> format(s,'*^10')
'***abc****'

>>> format(5,'^10')
'    5     '

>>> n = 5

>>> n.__format__('>10')
'         5'

>>> format(123,'+')
'+123'

>>> format(-123,'+')
'-123'

>>> format(-123,'>+10')
'      -123'

>>> format(-123,'=+10')
'-      123'

>>> format(-123,'0=+10')
'-000000123'

>>> format(546,'0=+10')
'+000000546'

>>> d = {
... 'lodDist':100.0,
... 'SmallCull':0.04,
... 'DistCull':500.0,
... 'trilinear':40
... 'farclip':477
... }
  File "<ipython-input-62-1aefeb87f281>", line 6
    'farclip':477
            ^
SyntaxError: invalid syntax


>>> d = {
... 'lodDist':100.0,
... 'SmallCull':0.04,
... 'DistCull':500.0,
... 'trilinear':40,
... 'farclip':477
... }

>>> w = max(map(len,d.keys()))

>>> for k,v in d.items():
...     print(k.ljust(w),':',v)
... 
lodDist   : 100.0
SmallCull : 0.04
DistCull  : 500.0
trilinear : 40
farclip   : 477

>>> 

原文地址:https://www.cnblogs.com/Richardo-M-Q/p/13284244.html