Python Data format:%s string,%d integer,%f floating-point

Source: Internet
Author: User

Tag: Address parameter floating point interception is 2.7 pytho port rule

Format character%
Percent percentile sign #就是输出一个%
%c character and its ASCII code
%s string
%d signed integer (decimal)
%u unsigned integer (decimal)
%o unsigned integer (octal)
%x unsigned integer (hexadecimal)
%x unsigned integer (16 uppercase characters)
%e floating-point numbers (scientific notation)
%E floating point number (scientific notation, E instead of e)
%f floating point number (with decimal point symbol)
%g floating-point numbers (%e or%f depending on the size of the value)
%G floating-point number (similar to%G)
%p pointer (memory address with hexadecimal print value)
%n the number of stored output characters into the next variable in the parameter list

%s string

string="Hello"        #%s The result is hello when printingPrint "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 HelloPrint "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,#so the print result of%7s is HelloPrint "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,#so the print result of%-7s is HelloPrint "string=%-7s!"% string#Output:string=hello!     #%.2s means to intercept the first 2 characters of a string, so%.2s prints the result of the HePrint "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 .#so the print result of%.7s is HelloPrint "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,#when the length of the truncated string is less than a, you also need to fill the left side of the spacePrint "string=%7.2s"% string#output:string= HePrint "string=%2.7s"% string#Output:string=helloPrint "string=%10.7s"% string#output:string= Hello    #You can also use%*.*s to represent precision, and the values of two * are specified in the first two digits of the following parentheses, respectivelyPrint "string=%*.*s"% (7,2,string)#output:string= He

%d integral type

Num=14#%d results when printingPrint "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 asPrint "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 isPrint "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 00014Print "num=%05d"% num#output:num=00014    #%.3d 3 After the decimal point means that the printed result is a 3-bit integer,#when the number of digits in an integer is not 3 bits, the left side of the integer is 0, so the%.3d print result is 014Print "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,#when the number of digits in an integer is not 3 bits, 0 is added to the left of the integer, so the%.3d print is 014Print "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,#The rule is to fill 0 priority, the final length selection of the larger one, so%5.3d printing results or 014Print "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,#because it is 05, and then 0 on the left, the final length of the selected value of the larger one, so%05.3d printing results or 00014Print "Num=%05.3d"% num#output:num=00014    #You can also use%*.*d to represent precision, and the values of two * are specified in the first two digits of the following parentheses, respectively#as follows, but this way 04 loses the function of 0, can only fill the space, only after the decimal point 3 to fill 0Print "num=%*.*d"% (04,3,num)#output:num= 014

%f floating Point type

ImportMath#%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    #only%f when the original value, the default is the number of 5 digits after the decimal pointPrint "pi=%f"% Math.PI#output:pi=3.141593    #just%9f, the print length of 9 digits, the decimal point also accounted for one bit, not enough left to fill the spacePrint "pi=%9f"% Math.PI#output:pi=_3.141593    #only. When there are no subsequent digits, the decimal output integer is removed, and 03 is not enough 3 digits to the left of 0Print "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 spacePrint "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 spacePrint "pi=%-6.3f"% Math.PI#output:pi=3.142_    #You can also use%*.*f to represent precision, and the values of two * are specified in the first two digits of the following parentheses, respectively#as follows, but this way 06 loses the function of 0, can only fill the spacePrint "pi=%*.*f"% (06,3,MATH.PI)#output:pi=_3.142

Practical

# generate a 9*9 multiplication table Print ('\ n''"%d*%d=%2s" for inch  for  in range (1,10)]))

Python Data format:%s string,%d integer,%f floating-point

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.