Python's%s string,%d integer,%f floating-point

Source: Internet
Author: User

%s

1string="Hello"    2         3     #%s The result is hello when printing4     Print "string=%s"% string#Output:string=hello5         6     #%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 Hello7     Print "string=%2s"% string#Output:string=hello8         9     #%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,Ten     #so the print result of%7s is Hello One     Print "string=%7s"% string#output:string= Hello A          -     #%-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 Hello the     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 . +     #so the print result of%.7s is Hello A     Print "string=%.7s"% string#Output:string=hello at          -     #%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 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 in          -     #You can also use%*.*s to represent precision, and the values of two * are specified in the first two digits of the following parentheses, respectively to     Print "string=%*.*s"% (7,2,string)#output:string= He

d%

1Num=142         3     #%d results when printing4     Print "num=%d"% num#output:num=145         6     #%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 as7     Print "num=%1d"% num#output:num=148         9     #%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 isTen     Print "num=%3d"% num#output:num= One          A     #%-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_ -          the     #%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, +     #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 014 -     Print "Num=%.3d"% num#output:num=014 +          A     #%.0003d 0003 and 3 after the decimal point represent 3, which means that the printed result is a 3-bit integer, at     #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 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, -     #The rule is to fill 0 priority, the final length selection of the larger one, so%5.3d printing results or 014 -     Print "Num=%5.3d"% num#output:num= 014 in          -     #%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, to     #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 00014 +     Print "Num=%05.3d"% num#output:num=00014 -          the     #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 0 $     Print "num=%*.*d"% (04,3,num)#output:num= 014

%f

1     ImportMath2         3     #%a.bf,a indicates the print length of the floating-point number, and B indicates the precision after the decimal point of the floating point4         5     #only%f when the original value, the default is the number of 5 digits after the decimal point6     Print "pi=%f"% Math.PI#output:pi=3.1415937         8     #just%9f, the print length of 9 digits, the decimal point also accounted for one bit, not enough left to fill the space9     Print "pi=%9f"% Math.PI#output:pi=_3.141593Ten          One     #only. When there are no subsequent digits, the decimal output integer is removed, and 03 is not enough 3 digits to the left of 0 A     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 the     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_ +          -     #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 space A     Print "pi=%*.*f"% (06,3,MATH.PI)#output:pi=_3.142

Python's%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.