这段代码有什么作用?
for x in range(1,11):
print('{0:2d} {1:3d} {2:4d}'.format(x,x**2,x**3))
Run Code Online (Sandbox Code Playgroud)
0: | 1: | 2: => The position in the arg list from which to get
the value. The order can be anything you want, and
you can repeat values, e.g. '{2:...} {0:...} {1:...} {0:...}'
2 | 3 | 4 => The minimum width of the field in which to display
the value. Right justified by default for numbers.
d => The value must be an integer and it will be displayed in
base 10 format (v. hex, octal, or binary format)
Run Code Online (Sandbox Code Playgroud)
下面是一个例子:
s = "{2:2d}\n{0:3d}\n{1:4d}".format(2, 4, 6)
print(s)
--output:--
6
2
4
Run Code Online (Sandbox Code Playgroud)