python - 格式化字符串
python - formatting a string
我想以性感对齐的方式打印字符串,但我看不出我做错了什么?
我想实现这个:
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
仅供参考 - 这样做:打印 '{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
它有点吃掉我中间的文本栏?
你需要定义第二个元素也是左对齐的,像这样
print '{0:<5} {1:<15} {2}'.format(...)
在这里,你说第一列左对齐,宽度为5,第二项也应该左对齐,宽度为15。
我想以性感对齐的方式打印字符串,但我看不出我做错了什么?
我想实现这个:
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
仅供参考 - 这样做:打印 '{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
它有点吃掉我中间的文本栏?
你需要定义第二个元素也是左对齐的,像这样
print '{0:<5} {1:<15} {2}'.format(...)
在这里,你说第一列左对齐,宽度为5,第二项也应该左对齐,宽度为15。