Python 格式化字符串

Python formatted String

程序 1:

print('{:-10}9'.format(12345))
print('{:-10}9'.format(8973955))

程序 1 的输出:

     123459
   89739559

程序 2:

print('{:10}9'.format(12345))
print('{:10}9'.format(8973955))

程序 2 的输出:

     123459
   89739559

这两个程序只有一处不同。在第一个程序中,我使用 -10 进行额外缩进。在第二个程序中,我使用 10 作为额外的缩进。 -10 和 10 都在左侧缩进。但我想在右侧做缩进,这样我就可以产生这样的输出:

12345     9
8973955   9

如何使用格式化字符串文字在右侧缩进

指定对齐的方向(对齐选项):

print('{:<10}9'.format(12345))
print('{:<10}9'.format(8973955))

输出:

12345     9
8973955   9

https://docs.python.org/3.4/library/string.html#format-specification-mini-language