str.format() -> 如何左对齐

11 投票
1 回答
12484 浏览
提问于 2025-04-15 11:25
>>> print 'there are {0:10} students and {1:10} teachers'.format(scnt, tcnt)
there are        100 students and         20 teachers

那么,代码应该怎么写,才能让输出变成:

there are 100        students and 20         teachers

谢谢。

相关问题:

1 个回答

23
print 'there are {0:<10} students and {1:<10} teachers'.format(scnt, tcnt)

以前的 % 操作符用 - 来对齐文本,而新的 format 方法则使用 <> 来进行对齐。

撰写回答