python字符串对齐格式

时间:2012-12-18 18:55:16

标签: python

我在python中有一些字符串,我想互相帮助,但似乎无法做到。

 a = ("adam, home, wednesday, 17th")
 b = ("Tiffany, office, monday, 9th")

我把它们分开了“,”

我想输出:

 adam         home     wednesday     17th
 tiffany      office   monday        9th

我正在尝试使用

 working_line =('{0} {1:<20} {2:<15} {3:<10}'.format(source,group,symbol,position))

这不起作用,我有点困惑......

2 个答案:

答案 0 :(得分:3)

{3}字段定义中缺少冒号:

working_line =('{0} {1:<20} {2:<15} {3:<10}'.format(source,group,symbol,position))

答案 1 :(得分:1)

你怎么做这样的事情:

a = ['adam', 'home', 'wednesday', '17th']
b = ['Tiffany', 'office', 'monday', '9th']

for x in [a, b]:
    working_line = ' '.join(map(lambda s: s.ljust(10, ' '), x))
    print working_line

这是输出:

adam       home       wednesday  17th      
Tiffany    office     monday     9th