{0:2d} {1:3d} {2:4d}'.format(x, x * x, x * x * x) understanding in Python

geniushorse 2022-11-08 04:25:15 阅读数:872

formatunderstandingpython

Code

for x in range(1, 11):print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))

Results

 1 1 12 4 83 9 274 16 645 25 1256 36 2167 49 3438 64 5129 81 72910 100 1000

Answer
The format() function
The parameters of the string are represented by {NUM}, 0, which means the first parameter, 1, which means the second parameter, which will be incremented in sequence;
{0:2d} represents the format of the first parameter x.0 for x, :2d for two-width decimal display.
{1:3d} represents the format of the first parameter x*x.1 represents x*x, :3d represents a decimal number display with three widths.
{2:4d} represents the format of the first parameter x*x*x.2 represents x*x*x, :4d represents the decimal display of four widths.

版权声明:本文为[geniushorse]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/312/202211080409467281.html