我想以一种性感的对齐方式打印一个字符串,但我看不出我做错了什么?
我想实现这个:
15 OBJECT (7) Content Placeholder 5
17 BODY (2) Text Placeholder 1
18 BODY (2) Text Placeholder 2
23 OBJECT (7) Content Placeholder 3
24 PICTURE (18) Picture Placeholder 4
25 BODY (2) Text Placeholder 6
我的代码:
for shape in slide.placeholders:
print '{0:<5} {1} {2:<5}'.format(shape.placeholder_format.idx,
shape.placeholder_format.type,
shape.name)
但这就是我得到的
15 OBJECT (7) Content Placeholder 5
17 BODY (2) Text Placeholder 1
18 BODY (2) Text Placeholder 2
23 OBJECT (7) Content Placeholder 3
24 PICTURE (18) Picture Placeholder 4
25 BODY (2) Text Placeholder 6
仅供引用 - 这样做: print '{0:<5} {1:<15} {2:<5}'.format(...)
给了我这个:
15 7 Content Placeholder 5
17 2 Text Placeholder 1
18 2 Text Placeholder 2
23 7 Content Placeholder 3
24 18 Picture Placeholder 4
25 2 Text Placeholder 6
它有点侵 eclipse 我的中间文本栏?
最佳答案
您需要将第二个元素定义为左对齐,如下所示
print '{0:<5} {1:<15} {2}'.format(...)
这里,你说第一列应该左对齐,宽度为5,第二项也应该左对齐,宽度为15。
关于python - 格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29235718/