tony selcuk
tony selcuk

Reputation: 687

Formatting printing with spacing Python

I am trying to have all the values indented and at the same level whilst printing the integers and strings specified below. I am trying to have the space equidistant throughout. How would I be able to do that?

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'))

Output

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

Expected Output:

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

Upvotes: 0

Views: 59

Answers (1)

Dust
Dust

Reputation: 425

You can specify the format for decimal number like this.

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'))

The ':<2d' in the format string specifies the number to be left justified, and space(in characters) it should take for it.

Upvotes: 1

Related Questions