"Reprint" Python%s%d%f

Source: Internet
Author: User

%s string

[Python]View PlainCopyPrint?
  1. string= "Hello"     
  2. #%s The result is hello when printing   
  3. Print "string=%s" % string # Output:string=hello
  4. #%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   
  5. Print "string=%2s" % string # Output:string=hello
  6. #%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,   
  7. #所以%7s Print result is Hello   
  8. Print "string=%7s" % string # output:string= Hello
  9. #%-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,   
  10. #所以%-7s Print result is Hello   
  11. Print "string=%-7s!" % String  # Output:string=hello!   
  12. #%.2s means to intercept the first 2 characters of a string, so%.2s prints the result of the He   
  13. Print "string=%.2s" % string # output:string=he
  14. #%.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 .   
  15. #所以%.7s Print result is Hello   
  16. Print "string=%.7s" % string # Output:string=hello
  17. #%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,   
  18. #当截取的字符串长度小于a时, you also need to fill the left side of the space   
  19. Print "string=%7.2s" % string # output:string= He
  20. Print "string=%2.7s" % string # Output:string=hello
  21. Print "string=%10.7s" % string # output:string= Hello
  22. #还可以用%*.*s to represent precision, the values of two * are specified in the first two digits of the following parentheses, respectively   
  23. 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?
  1. num= -     
  2. #%d The result when printing   
  3. Print "num=%d" % num # output:num=14
  4. #%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   
  5. Print "num=%1d" % num # output:num=14
  6. #%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   
  7. Print "num=%3d" % num # output:num=
  8. #%-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_   
  9. Print "num=%-3d" % num # output:num=14_
  10. #%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   
  11. Print "num=%05d" % num # output:num=00014
  12. #%.3d 3 After the decimal point means that the printed result is a 3-bit integer,   
  13. #当整数的位数不够3位时, the left side of the integer is 0, so the%.3d print result is 014   
  14. Print "Num=%.3d" % num # output:num=014
  15. #%.0003d 0003 and 3 after the decimal point represent 3, which means that the printed result is a 3-bit integer,   
  16. #当整数的位数不够3位时, 0 is added to the left of the integer, so the%.3d print result is 014   
  17. Print "num=%.0003d" % num # output:num=014
  18. #%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,   
  19. #规则就是补0优先, the final length of the selection of the larger one, so%5.3d printing results or 014   
  20. Print "Num=%5.3d" % num # output:num= 014
  21. #%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,   
  22. #由于是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   
  23. Print "Num=%05.3d" % num # output:num=00014
  24. #还可以用%*.*d to represent precision, the values of two * are specified in the first two digits of the following parentheses, respectively   
  25. #如下, but this way 04 lost the function of 0, can only fill the space, only 3 after the decimal point to fill 0   
  26. 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?
  1. Import Math
  2. #%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   
  3. #只是%f is the original value, the default is 5 digits after the decimal point   
  4. Print "pi=%f" % Math.PI # output:pi=3.141593
  5. #只是%9f, the print length of 9 digits, the decimal point also accounted for one bit, not enough left to fill the space   
  6. Print "pi=%9f" % Math.PI # output:pi=_3.141593
  7. #只有. When there is no subsequent number, the decimal output integer is removed, and 03 indicates insufficient 3 digits to the left of 0   
  8. Print "pi=%03.f" % Math.PI # output:pi=003
  9. #%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   
  10. Print "pi=%6.3f" % Math.PI # output:pi=_3.142
  11. #%-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   
  12. Print "pi=%-6.3f" % Math.PI # output:pi=3.142_
  13. #还可以用%*.*f to represent precision, the values of two * are specified in the first two digits of the following parentheses, respectively   
  14. #如下, but this way 06 loses the function of 0, can only fill the space   
  15. 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

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.