Basics of getting started with python--passing parameters

[python] view plain copy
1.string=”hello”
2.
3.#%s print result is hello
4.print “string=%s” % string # output: string=hello
5.
6.#%2s meaning The length of the string is 2. When the length of the original string exceeds 2, it will be printed according to the original length, so the print result of %2s is still hello
7.print “string=%2s” % string # output: string=hello
8.
9 .#%7s means that the length of the string is 7. When the length of the original string is less than 7, a space is added to the left of the original string.
10.# So the print result of %7s is hello
11.print “string=%7s” % string # output: string= hello
12.
13.#%-7s means that the length of the string is 7. When the length of the original string is less than 7, a space is added to the right of the original string,
14.# So the length of %-7s The print result is hello
15.print “string=%-7s!” % string # output: string=hello !
16.
17.#%.2s means to intercept the first 2 characters of the string, so the print result of %.2s is he
18.print "string=%.2s" % string # output: string=he
19.
20.#%.7s means to intercept the first 7 characters of the string. When the length of the original string is less than 7, it is the string itself.
21.# So the print result of %.7s is hello
22.print “string= %.7s” % string # output: string=hello
23.
24.#%a.bs This format is a combination of the above two formats, first intercept the string according to the number b after the decimal point,
25.# When the intercepted character When the length of the string is less than a, a space needs to be added to the left
. 26.print “string=%7.2s” % string # output: string= he
27.print “string=%2.7s” % string # output: string=hello
28.print "string=%10.7s" % string # output: string= hello
29.
30.# You can also use % .*s to indicate the precision, the two values ​​are respectively specified in the first two digits of the parentheses after
31 .print “string=%*.*s” % (7,2,string) # output: string= he

%d integer

[python] view plain copy
1.num=14
2.
3.#%d print result is 14
4.print "num=%d" % num # output: num=14
5.
6.#%1d means print The result is a 1-digit integer. When the number of digits of the integer exceeds 1, the original value of the integer is printed, so the print result of %1d is still 14
7.print “num=%1d” % num # output: num=14
8.
9 .#%3d means that the print result is a 3-digit integer. When the number of digits in the integer is less than 3 digits, a space is added to the left of the integer, so the print result of %3d is 14
10.print “num=%3d” % num # output: num= 14
11.
12.#%-3d means that the print result is a 3-digit integer. When the number of digits in the integer is less than 3 digits, a space is added to the right of the integer, so the print result of %3d is 14_
13.print "num=%-3d" % num # output: num=14_
14.
15.#%05d means that the print result is a 5-digit integer. When the number of digits in the integer is not enough, 0 is added to the left of the integer, so % The print result of 05d is 00014
16.print “num=%05d” % num # output: num=00014
17.
18.#%.3d The 3 after the decimal point means that the print result is a 3-digit integer,
19.# When the integer When the number of digits is not enough, 0 is added to the left of the integer, so the print result of %.3d is 014
20.print “num=%.3d” % num # output: num=014
21.
22.#%.0003d The 0003 after the decimal point is the same as 3, which means 3, which means the print result is a 3-digit integer,
23.# When the number of digits in the integer is not enough to 3 digits, 0 is added to the left of the integer, so the print result of %.3d is still 014
24.print “num=%.0003d” % num # output: num=014
25.
26.#% 5.3d is a combination of the two filling methods. When the number of digits of the integer is not enough to 3, first fill with 0 on the left, and when it is still less than 5 digits, fill in the space on the left. The
27.# rule is to fill 0 first, and finally Select the one with the larger value for the length of %5.3d, so the print result of %5.3d is still 014
28.print “num=%5.3d” % num # output: num= 014
29.
30.#%05.3d are two filling methods The synthesis, when the number of digits of the integer is less than 3, first fill 0 on the left, or when it is not enough 5 digits,
31.# Since it is 05, and then fill 0 on the left, the final length chooses the one with the larger value, so The print result of %05.3d is still 00014
32.print “num=%05.3d” % num # output: num=00014
33.
34. # You can also use % .*d to indicate the precision, the two values ​​are smaller at the back The first two digits of the parentheses are specified as
35.# as follows, but in this way, 04 loses the function of filling 0, only spaces can be filled, and only 3 after the decimal point can be filled with 0
36.print “num=%*.*d” % (04,3,num) # output: num= 014

%f float

[python] view plain copy
1.import math
2.
3.#%a.bf, a represents the printing length of the floating point number, b represents the precision behind the decimal point of the floating point number
4.
5.# is only the original value when %f, the default It is 5 digits after the decimal point
6.print "PI=%f" % math.pi # output: PI=3.141593
7.
8.# When it is only %9f, it means that the printing length is 9 digits, and the decimal point also occupies one place, which is not enough left Fill with space on the side
9.print "PI=%9f" % math.pi # output: PI=_3.141593
10.
11.# Only. When there is no following number, it means that the decimal is removed and the integer is output, and 03 means that there are not enough left 3 digits Side-fill 0
12.print “PI=%03.f” % math.pi # output: PI=003
13.
14.#%6.3f means that the decimal point is accurate to 3 digits, and the total length is 6 digits, including the decimal point, which is not enough Fill space on the left
15.print "PI=%6.3f" % math.pi # output: PI=_3.142
16.
17.#%-6.3f means that the decimal point is accurate to 3 digits, and the total length is 6 digits, including Decimal point, not enough space on the right side
18.print "PI=%-6.3f" % math.pi # output: PI=3.142_
19.
20.# You can also use % .*f to indicate precision, twoThe value of 21.# is specified as follows in the first two digits of the parentheses
, but in this way, 06 loses the function of filling 0, and can only fill in spaces
22.print “PI=%*.*f” % (06, 3,math.pi) # output: PI=_3.142

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324822502&siteId=291194637