string= "Hello" #%s the result is Hello print "string=%s"% string # Output:string=hello #%2s meaning is the string length of 2, when the length of the original string is longer than 2 o'clock, printed according to the original length, so the%2s print result is the Hello print "string=%2s"% string # Output:string=hello #%7 s means the string length is 7, when the length of the original string is less than 7 o'clock, the left of the original string to fill the space, #所以%7s print is hello print "string=%7s"% string # output:string= Hello #%-7s meaning is the string length is 7, when the original string length is less than 7 o'clock, the original string to the right to fill the space, #所以%-7s printing result is hello print "string=%-7s!"% stri
NG # 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 words of an intercept string
Character, when the original string length is less than 7 o'clock, that is, the string itself, #所以%.7s printing result is hello print "string=%.7s"% string # Output:string=hello #%a.bs This format is the combination of the above two formats, first, according to the number after the decimal point to intercept the string, #当截取的字符串长度小于a时, but 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 print" string=%*.*s "% (7,2,string) # output:string= he%d int [python] view plain copy num=14 #%d print results are PR int "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 as an integer, so the result of the%1d printing is the print The "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 "Nu" m=%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, the right side of the integer is filled with space, so the%3d print is 14_ print" 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" Nu
m=%05d "% num # output:num=00014 #%.3d the 3 meaning after the decimal point is to print the result to a 3-bit integer, #当整数的位数不够3位时, to the left of the integer to make up 0, so the%.3d print result is 014 The print "Num=%.3d"% num # output:num=014 #%.0003d 0003 and 3 after the decimal point, all representing 3, meaning that the result is a 3-bit integer, #当整数 When the number of digits is not 3 digits, the left side of the integer is 0, so the%.3dPrint results or 014 print "num=%.0003d"% num # output:num=014 #%5.3d is a combination of the two methods of completion, when the number of digits is not enough 3 o'clock, first on the left 0, or not enough 5, and then in
Left to fill the space, #规则就是补0优先, the final length of the selected value of the larger one, so%5.3d printing results or 014 print "Num=%5.3d"% num # output:num= 014 #%05.3d is the 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, #由于是05, and then on the left to fill 0, the final length of the larger one, so%05.3d print results or 00014 print The "Num=%05.3d"% num # output:num=00014 #还可以用%*.*d to represent precision, with two * values specified #如下 in the first two digits of the following parentheses, but this way 04 loses the function of 0.
Only space can be filled, only 3 after the decimal point can complement 0 print "Num=%*.*d"% (04,3,num) # output:num= 014%f floating-point [python] view plain copy The import math #%a.bf,a represents the print length of the floating-point number, and B indicates the precision #只是%f after the floating-point decimal point, which is the default 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 # OU tput:pi=_3.141593 #只有. When there is no following number, the decimal output integer is removed, and 03 indicates that it is not enough 3 digits left to complement 0 print "PI=%03.F"% Math.PI # OUTPUT:PI =003 #%6.3f represents decimalPoint back to 3 digits, total length of 6 digits, including 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 after the precision 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 represent precision, two * values are small in the back The first two digits of parentheses specify #如下, but this way 06 loses the function of 0, only the space print "pi=%*.*f"% (06,3,MATH.PI) # output:pi=_3.142