使用间距 Python 格式化打印

Formatting printing with spacing Python

我试图在打印下面指定的整数和字符串时使所有值缩进并处于同一级别。我试图让 space 始终保持等距。我怎样才能做到这一点?

print('Consecutive monthly {} results: {}\tIndexes: {} - {}'.format('positive',14,'2019-04-01 00:00:00','2020-06-01 00:00:00'))
print('Consecutive monthly {} results: {}\tIndexes: {} - {}'.format('negative',2,'2018-02-01 00:00:00','2020-06-01 00:00:00'))

输出

Consecutive monthly positive results: 14    Indexes: 2019-04-01 00:00:00 - 2020-06-01 00:00:00
Consecutive monthly negative results: 2 Indexes: 2018-02-01 00:00:00 - 2018-03-01 00:00:00

预期输出:

Consecutive monthly positive results: 14    Indexes: 2019-04-01 00:00:00 - 2020-06-01 00:00:00
Consecutive monthly negative results: 2     Indexes: 2018-02-01 00:00:00 - 2018-03-01 00:00:00

您可以像这样指定十进制数的格式。

print('Consecutive monthly {} results: {:<2d}\tIndexes: {} - {}'.format('positive',14,'2019-04-01 00:00:00','2020-06-01 00:00:00'))
print('Consecutive monthly {} results: {:<2d}\tIndexes: {} - {}'.format('negative',2,'2018-02-01 00:00:00','2020-06-01 00:00:00'))

格式字符串中的“:<2d”指定要左对齐的数字,space(以字符为单位)它应该用于它。