Transfer from Python%s%d%f, reprint standby, if tort, please contact bloggers Delete
%s
string= "Hello" #%s printing results in hello print "string=%s"% string # Output:string=hello #%2s meaning that the string length is 2, when the length of the original string exceeds 2 o'clock, print according to the original length, so%2s's print result is also hello print "string=%2s"% string # Output:string=hello #%7s meaning that the string length is 7, when the length of the original string is less than 7 o'clock, in the original character String left blank, #所以%7s print result is hello print "string=%7s"% string # output:string= Hello #%-7s meaning is string length 7, when the length of the original string is less than 7 o'clock
, fill the space on the right side of the original string, #所以%-7s Print the result is Hello print "string=%-7s!"% string # Output:string=hello! #%.2s means to intercept the first 2 characters of a string, so the%.2s print is the He print "string=%.2s"% string # Output:string=he #%.7s means the first 7 characters of an intercept string, when the original string length is small At 7 o'clock, that is, the string itself, #所以%.7s's print result is hello print "string=%.7s"% string # Output:string=hello #%a.bs This format is a synthesis of the above two formats, starting with the small After the number of points B intercept the string, #当截取的字符串长度小于a时, you also need to fill the left space print "string=%7.2s"% string # output:string= He print string=%2.7s "% string # Output:string=hello print" string=%10.7s "% string # output:string= Hello #还可以用%*.*s to represent precision, two * Values are specified in the first two digits of the following parentheses to specify the print "string=%*.*s"% (7,2,string) # output:string= He
%d integral type
num=14 #%d Printing results in print "num=%d"% num # output:num=14 #%1d means that the result is a 1-bit integer, and when the number of digits in the integer is more than 1 digits, the original value is printed by integer, so%1d The printout of the print "num=%1d"% num # output:num=14 #%3d means that the result is a 3-bit integer, and when the number of digits in the integer is less than 3 digits, a space is filled on the left of the integer so that the%3d print result is P Rint "num=%3d"% num # output:num= #%-3d means that the result is a 3-bit integer, and when the number of digits in the integer is less than 3 digits, a space is filled on the right of the integer, so the%3d print is 14_ prints "num=% -3d "% num # output:num=14_ #%05d means that the result is a 5-bit integer, and when the number of digits in the integer is less than 5, the left side of the integer is 0, so the%05d print result is 00014 print" num=%05d "% nu M # output:num=00014 #%.3d 3 meaning after the decimal point is to print a 3-bit integer, #当整数的位数不够3位时, to the left of the integer 0, so%.3d print is 014 print "Num=%.3d"% Num # output:num=014 #%.0003d the 0003 and 3 after the decimal point, all representing 3, meaning that the printout is 3-bit integer, #当整数的位数不够3位时, to the left of the integer 0, so%.3d print results or 014 PR int "num=%.0003d"% num # output:num=014 #%5.3d is a combination of two ways to complement, when the number of digits is not enough 3 o'clock, first on the left 0, or not enough 5, then the left to fill space, #规则就是补0优先, the final length Select the larger value, so the%5.3d print or 014 print "Num=%5.3d"% num # output:num= 014 #%05.3d is a combination of two ways to complement, when the number of digits is not enough 3 o'clock, first on the left 0, or not enough for 5, #由于是05, and then on the left0, the final length of the selected value of the larger one, so the%05.3d print result is 00014 print "Num=%05.3d"% num # output:num=00014 #还可以用%*.*d to indicate the accuracy, two * values are respectively The first two digits of the bracket specify #如下, but this way 04 loses the function of 0, can only fill the space, only 3 after the decimal point to fill 0 print "num=%*.*d"% (04,3,num) # output:num= 014
%f
Import Math
#%a.bf,a represents the print length of the floating-point number, B indicates the precision #只是%f after the floating-point decimal point
, and the default is 5 digits after the decimal point print
"pi=%f"% Math.PI # output:pi=3.141593
#只是%9f, the print length of 9 digits, the decimal point also occupies a bit, not enough left to fill
the space print "pi=%9f"% Math.PI # output:pi=_3.141593
#只有. When there is no later number, the decimal output integer is removed, 03 is not enough 3 digits left to complement 0
print "pi=%03.f"% Math.PI # output:pi=003
#% 6.3f indicates that the decimal point is accurate to 3 digits, the total length of 6 digits, including the decimal point, not enough left to fill
the space print "pi=%6.3f"% Math.PI # output:pi=_3.142
#%- 6.3f indicates that the decimal point is accurate to 3 digits, the total length of 6 digits, including the decimal point, not enough to the right complement blank
print "pi=%-6.3f"% Math.PI # output:pi=3.142_
#还可以用%*.* F to indicate precision, two * values in the following parentheses in the first two digits of the value specified
#如下, but this way 06 lost the function of 0, can only complement the space
print "pi=%*.*f"% (06,3,math.pi) # output: pi=_3.142