1
I’m studying about format strings and I’m not getting the same alignment behavior on the left with the minus sign (-
) using the function format
. What is the right way to do this using format
?
>>> for x in range(1, 11):
... print('%-2d %-3d %-4d %-6d' % (x, x**2, x**3, x**4))
...
1 1 1 1
2 4 8 16
3 9 27 81
4 16 64 256
5 25 125 625
6 36 216 1296
7 49 343 2401
8 64 512 4096
9 81 729 6561
10 100 1000 10000
>>> for x in range(1, 11):
... print('{0:-2d} {1:-3d} {2:-4d} {3:-6d}'.format(x, x**2, x**3, x**4))
...
1 1 1 1
2 4 8 16
3 9 27 81
4 16 64 256
5 25 125 625
6 36 216 1296
7 49 343 2401
8 64 512 4096
9 81 729 6561
10 100 1000 10000
I will leave the https://pyformat.info/ link if anyone wants a more simplified "documentation".
– fernandosavio