如何使用样式设置文本格式

alp*_*ric 3 python

下面的代码:

info={'Resolution':'640x360', 'DisplayResolution': '640x360', 'Display Channels':'R,G,B,A'}
for key in info:
    print (key + str(info[key].rjust(45,'.')))
Run Code Online (Sandbox Code Playgroud)

产生以下输出:

Resolution......................................640x360
DisplayResolution......................................640x360
Display Channels......................................R,G,B,A
Run Code Online (Sandbox Code Playgroud)

但我想得到:

Resolution.............................................640x360
DisplayResolution......................................640x360
Display Channels.......................................R,G,B,A
Run Code Online (Sandbox Code Playgroud)

怎么做到这一点?

编辑:

感谢大家的宝贵意见.以下是根据您的建议汇总的代码摘要:

ROW_SIZE=0
for key, value in info.iteritems():
    if not key or not value: continue
    key=str(key)
    value=str(value)
    total=len(key)+len(value)+10
    if ROW_SIZE<total: ROW_SIZE=total

result=''
if ROW_SIZE:
    for key in info:
        result+=(key+str(info[key]).rjust(ROW_SIZE-len(key),'.'))+'\n'
print result
Run Code Online (Sandbox Code Playgroud)

unu*_*tbu 8

将期间作为关键字的填充,而不是值:

info = {'Resolution':'640x360', 'DisplayResolution': '640x360',
      'Display Channels':'R,G,B,A'}
for key, value in info.items():
    print('{k:.<55}{v}'.format(k=key, v=value))
Run Code Online (Sandbox Code Playgroud)

产量

Resolution.............................................640x360
DisplayResolution......................................640x360
Display Channels.......................................R,G,B,A
Run Code Online (Sandbox Code Playgroud)

以上使用较新的格式方法.或者,使用旧式字符串格式:

for key,value in info.items():
    print('%s%s' % (key.ljust(55, '.'), value))
Run Code Online (Sandbox Code Playgroud)