Your format is easily translated to the str.format()
formatting syntax:
print("{:>3s} {:20s} {:>12s}".format("Id", "State", "Population"))
print("{id:3d} {name:20s} {population:12d}".format(**state))
Note that left-alignment is achieved by prefixing the width with <
, not -
, and default alignment for strings is to left-align, so a >
is needed for the header strings and the <
can be omitted, but otherwise the formats are closely related.
This extracts the values directly from the state
dictionary by using the keys in the format itself.
You may as well just use the actual output result of the first format directly:
print(" Id State Population")
Demo:
>>> state = {'id': 15, 'name': 'New York', 'population': 19750000}
>>> print("{:>3s} {:20s} {:>12s}".format("Id", "State", "Population"))
Id State Population
>>> print("{id:3d} {name:20s} {population:12d}".format(**state))
15 New York 19750000