%s string
[Python]View PlainCopyPrint?
- string= "Hello"
- #%s The result is hello when printing
- Print "string=%s" % string # Output:string=hello
- #%2s means that the string length is 2, and when the original string is longer than 2 o'clock, it is printed at the original length, so the%2s print result is still Hello
- Print "string=%2s" % string # Output:string=hello
- #%7s means the string length is 7, when the length of the original string is less than 7 o'clock, the left side of the original string is blank,
- #所以%7s Print result is Hello
- Print "string=%7s" % string # output:string= Hello
- #%-7s means string length is 7, when the length of the original string is less than 7 o'clock, the right side of the original string to fill the space,
- #所以%-7s Print result is Hello
- Print "string=%-7s!" % String # Output:string=hello!
- #%.2s means to intercept the first 2 characters of a string, so%.2s prints the result of the He
- Print "string=%.2s" % string # output:string=he
- #%.7s means to intercept the first 7 characters of a string, which is the string itself when the original string is less than 7 o'clock long .
- #所以%.7s 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 number B after the decimal point to intercept the string,
- #当截取的字符串长度小于a时, you also need to fill the left side of the 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, the values of two * are specified in the first two digits of the following parentheses, respectively
- Print "string=%*.*s" % (7,2, String) # OUTPUT:STR ing= He
string= "Hello" #%s prints when the result is Hello print "string=%s"% string # Output:string=hello #%2s meaning string length is 2, when the length of the original string is more than 2 o'clock, press the original Length printing, so%2s print results or hello print "string=%2s"% string # Output:string=hello #%7s meaning the string length is 7, when the length of the original string is less than 7 o'clock, the left side of the original string to fill the space, #所以%7s print result is hello print "string=%7s"% string # output:string= Hello #%-7s meaning string length is 7, when the length of the original string is less than 7 o'clock, the right side of the original string to fill the space, #所以%-7s print result is hello print "string=%-7s!"% string # Output:string=hello! #%.2s means to intercept the first 2 characters of a string, so%.2s prints the result of he print "string=%.2s"% string # Output:string=he #%.7s meaning to intercept the first 7 characters of a string when the original string is less than 7 o'clock , that is, the string itself, #所以%.7s print result is hello print "string=%.7s"% string # Output:string=hello #%a.bs This format is a synthesis of the above two formats, first based on the number of digits after the decimal point B Truncate the string, #当截取的字符串长度小于a时, and also need to fill a space on its left 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 respectively in the first two of the following parentheses Bit value specifies print "string=%*.*s"% (7,2,string) # output:sTring= He
%d integral type
[Python]View PlainCopyPrint?
- num= -
- #%d The result when printing
- Print "num=%d" % num # output:num=14
- #%1d means that the printed result is a 1-bit integer, and when the number of digits in the integer exceeds 1 bits, the original value is printed, so the%1d is printed as
- Print "num=%1d" % num # output:num=14
- #%3d means that the printed result is a 3-bit integer, and when the number of digits in the integer is not 3 bits, the left side of the integer is blank, so the print result of the%3d is
- Print "num=%3d" % num # output:num=
- #%-3d means that the printed result is a 3-bit integer, and when the number of digits in the integer is not 3 bits, the right side of the integer is blank, so the%3d print is 14_
- Print "num=%-3d" % num # output:num=14_
- #%05d means that the printed result is a 5-bit integer, and when the number of digits in the integer is not 5 bits, the left side of the integer is 0, so the%05d print is 00014
- Print "num=%05d" % num # output:num=00014
- #%.3d 3 After the decimal point means that the printed result is a 3-bit integer,
- #当整数的位数不够3位时, the left side of the integer is 0, so the%.3d print result is 014
- Print "Num=%.3d" % num # output:num=014
- #%.0003d 0003 and 3 after the decimal point represent 3, which means that the printed result is a 3-bit integer,
- #当整数的位数不够3位时, 0 is added to the left of the integer, so the%.3d print result is 014
- Print "num=%.0003d" % num # output:num=014
- #%5.3d is a combination of two methods of completion, when the number of digits is not enough 3 o'clock, first on the left 0, or not enough 5 bits, and then on the left to fill the space,
- #规则就是补0优先, the final length of the selection of the larger one, so%5.3d printing results or 014
- Print "Num=%5.3d" % num # output:num= 014
- #%05.3d is a combination of two methods of completion, when the number of digits is not enough 3 o'clock, first on the left 0, or not enough 5 bits,
- #由于是05, and then on the left side of 0, the final length of the selection of the larger one, so%05.3d printing results or 00014
- Print "Num=%05.3d" % num # output:num=00014
- #还可以用%*.*d to represent precision, the values of two * are specified in the first two digits of the following parentheses, respectively
- #如下, but this way 04 lost 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  
num=14 #%d Printing results in print "num=%d"% num # output:num=14 #%1d means that the printed result is a 1-bit integer, and when the number of digits in the integer exceeds 1 bits, the output of the%1d is printed by the integer original value, so or the print "num=%1d"% num # output:num=14 #%3d means that the printed result is a 3-bit integer, and when the number of digits in the integer is not 3 bits, the left side of the integer is blank, so the%3d printing result is "num =%3d "% num # output:num= #%-3d means that the printed result is a 3-bit integer, when the number of digits is not enough 3 bits, the right side of the integer to fill the space, so%3d printing result is 14_ print" num=%-3d "% num # output:num=14_ #%05d means that the printed result is a 5-bit integer, when the number of digits is not 5 bits, the left side of the integer 0, so the%05d print result is 00014 print "num=%05d"% num # OU tput:num=00014 #%.3d 3 After the decimal point means that the print result is a 3-bit integer, #当整数的位数不够3位时, 0 on the left of the integer, so the%.3d print result is 014 print "Num=%.3d"% num # OUTP ut:num=014 #%.0003d 0003 and 3 after the decimal point, both represent 3, meaning that the printed result is a 3-bit integer, #当整数的位数不够3位时, to the left of the integer 0, so the%.3d printing result is 014 print "num=%.0003d"% num # output:num=014 #%5.3d is a combination of two ways to fill, when the number of digits is not enough 3 o'clock, first in the left 0, or not enough 5 bits, and then on the left to fill the space, #规则就是补0优先, the final length of the larger selection of the value of that, so%5.3d printing results or 0 Print "Num=%5.3d"% num # output:num= 014 #%05.3d is a combination of two ways to fill, when the number of digits is not enough 3 o'clock, first in the left 0, or not enough 5 bits, #由于是05, and then on the left 0, the final The length of the selected value of the larger one, so%05.3d printing resultsor 00014 print "Num=%05.3d"% num # output:num=00014 #还可以用%*.*d to represent precision, two * values are specified #如下 in the first two digits of the following parentheses, but this way 04 loses the complement of 0 Can, can only fill the space, only 3 after the decimal point to fill 0 print "num=%*.*d"% (04,3,num) # output:num= 014
%f floating Point type
[Python]View PlainCopyPrint?
- Import Math
- #%a.bf,a indicates the print length of the floating-point number, and B indicates the precision after the decimal point of the floating point
- #只是%f is the original value, 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 accounted for one bit, not enough left to fill the space
- Print "pi=%9f" % Math.PI # output:pi=_3.141593
- #只有. When there is no subsequent number, the decimal output integer is removed, and 03 indicates insufficient 3 digits to the left of 0
- Print "pi=%03.f" % Math.PI # output:pi=003
- #%6.3f indicates that the decimal point is accurate to 3 digits, with a total length of 6 digits, including a decimal point, not enough left fill space
- Print "pi=%6.3f" % Math.PI # output:pi=_3.142
- #%-6.3f indicates that the decimal point is accurate to 3 digits, with a total length of 6 digits, including a decimal point, not enough right fill space
- Print "pi=%-6.3f" % Math.PI # output:pi=3.142_
- #还可以用%*.*f to represent precision, the values of two * are specified in the first two digits of the following parentheses, respectively
- #如下, but this way 06 loses the function of 0, can only fill the space
- print "pi=%*.*f" % ( 06 , 3 , Math.PI) # OUTPUT: PI=_3.142  
The import math #%a.bf,a represents the print length of the floating-point number, b indicates the precision after the decimal point of the float #只是%f, 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 accounted for one bit, not enough left to fill the blank print "pi=%9f"% Math.PI # output:pi=_3.141593 #只有. When there is no subsequent number, it means that the decimal output integer is removed, 03 is not enough 3 digits left 0 print "pi=%03.f"% Math.PI # output:pi=003 #% 6.3f means the decimal point is accurate to 3 bits, the total length 6 digits, including the decimal point, not enough left to fill the blank print "pi=%6.3f"% Math.PI # output:pi=_3.142 #%- 6.3f means the decimal point is accurate to 3 bits, the total length 6 digits, including the decimal point, not enough right fill blank print "pi=%-6.3f"% Math.PI # output:pi=3.142_ #还可以用%*.* F to express the accuracy, the values of two * are specified in the first two digits of the following parentheses #如下, but this way 06 loses the function of 0, can only fill the space print "pi=%*.*f"% (06,3,math.pi) # output: pi=_3.142
"Reprint" Python%s%d%f