The%s%d%f of Python

Source: Internet
Author: User

%s string

  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:string= He

%d integral type

  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"% (3,num) # output:num= 014

%f floating Point type

    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"% (3,math.pi) # output:pi=_3.142
    16. Add buckle group 813622576 get free information we exchange Learning!!!!

The%s%d%f of Python

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.