Python string formatting % s % d % f, python string % s % d % f

Source: Internet
Author: User

Python string formatting % s % d % f, python string % s % d % f

As for the discussion of output formatting, the small editor was not just on the rise. When I was learning python, I often encountered "% d" for output. I have never studied it carefully. Today I saw it again, the following is a simple example. python outputs the 99 multiplication table:

#!/usr/bin/python# -*- coding: UTF-8 -*- for i in range(1, 10):  print   for j in range(1, i+1):    print "%d*%d=%d" % (i, j, i*j),

Result:

1*1 = 1
2*1 = 2 2*2 = 4
3*1 = 3 3*2 = 6 3*3 = 9
4*1 = 4 4 4*2 = 8 4*3 = 12 4*4 = 16
5*1 = 5 5*2 = 10 5*3 = 15 5*4 = 20 5*5 = 25
6*1 = 6 6*2 = 12 6*3 = 18 6*4 = 24 6*5 = 30 6*6 = 36
7*1 = 7 7*2 = 14 7*3 = 21 7*4 = 28 7*5 = 35 7*6 = 42 7*7 = 49
8*1 = 8 8*2 = 16 8*3 = 24 8*4 = 32 8*5 = 40 8*6 = 48 8*7 = 56 8*8 = 64
9*1 = 9 9*2 = 18 9*3 = 27 9*4 = 36 9*5 = 45 9*6 = 54 9*7 = 63 9*8 = 72 9*9 = 81
>>>

Next we will study the specific content of string output formatting.

% S string
When printing string = "hello" # % s, the result is hello print "string = % s" % string # output: string = hello # % 2 s, which indicates that the string length is 2, when the length of the original string exceeds 2, it is printed according to the original length. Therefore, the output of % 2s is hello print "string = % 2 s" % string # output: string = hello # % 7s indicates that the string length is 7. When the length of the original string is less than 7, fill in spaces on the left of the original string, # therefore, the print result of % 7s is hello print "string = % 7 s" % string # output: string = hello # %-7s, which means that the string length is 7, when the length of the original string is less than 7, fill in space on the right of the original string, # The print result of %-7s is hello print "string = %-7 s! "% String # output: string = hello! # %. 2 S refers to intercepting the first two characters of the string, so %. the printed result of 2 S is he print "string = %. 2 s "% string # output: string = he # %. 7 S refers to intercepting the first seven characters of a string. when the length of the original string is less than 7, it is the string itself, # So %. the 7 S print result is hello print "string = %. 7 s "% string # output: string = hello # %. bs is a combination of the above two formats. First, the string is intercepted based on the number B after the decimal point. # when the length of the string to be intercepted is smaller than, print "string = % 7.2 s" % string # output: string = he print "string = % 2.7 s" % string # output: string = hello print "string = % 10.7 s" % string # output: string = hello # % * can also be used *. * s indicates the precision. The two * values specify print "string = % * in the first two digits of the parentheses *. * s "% (7,2, string) # output: string = he
% D integer
When num = 14 # % d is printed, the result is 14 print "num = % d" % num # output: num = 14 # % 1d, which indicates that the print result is a one-digit integer, when the number of digits of an integer exceeds 1, it is printed based on the original value of the integer. Therefore, the print result of % 1d is still 14 print "num = % 1d" % num # output: num = 14 # % 3d indicates that the print result is a three-digit integer. When the number of digits of an integer is less than three, fill in spaces on the left side of the integer, therefore, the print result of % 3d is 14 print "num = % 3d" % num # output: num = 14 # %-3d, which means that the print result is a three-digit integer, when the number of digits of an integer is less than three digits, spaces are filled on the right side of the integer. Therefore, the print result of % 3d is 14 _ print "num = %-3d" % num # output: num = 14 _ # % 05d indicates that the print result is a 5-digit integer. If the number of digits of an integer is less than 5, add 0 to the left of the integer, therefore, the print result of % 05d is 00014 print "num = % 05d" % num # output: num = 00014 # %. 3 after the 3d decimal point indicates that the printed result is a three-digit integer. # When the number of digits of an integer is less than 3, add 0 to the left side of the integer, so %. the 3d printing result is 014 print "num = %. 3d "% num # output: num = 014 # %. 0003 and 3 after the decimal point in 3D indicate 3, indicating that the printed result is a three-digit integer. # When the number of digits in an integer is less than 3, add 0 to the left of the integer, so %. the 3d printing result is still 014 print "num = %. 0003d "% num # output: num = 014 # % 5.3d is a combination of the two filling methods. When the number of digits of an integer is less than 3, add 0 to the left, if the number is less than five, fill in spaces on the left. # The rule is to add 0 first. The final length is the one with a large value, therefore, the % 5.3d printing result is still 014 print "num = % 5.3d" % num # output: num = 014 # % 05.3d is a combination of the two methods, when the number of digits in an integer is less than 3, the value is 0 on the left or 5 digits on the left. # because the value is 05, the value is 0 on the left, the final length is the one with a large value, so the print result of % 05.3d is still 00014 print "num = % 05.3d" % num # output: num = 00014 # % *. * d is used to indicate the precision. The two * values are respectively specified in the first two digits of the parentheses # below. However, in this method 04, the 0 population function is lost and only spaces can be filled, only 3 after the decimal point can fill 0 print "num = % *. * d "% (04,3, num) # output: num = 014
% F floating point type
Import math # %. bf, a indicates the print length of the floating point number, and B indicates the precision after the floating point number. # It indicates the original value only when % f is used. The default value is print "PI = % f" % math after the decimal point. pi # output: PI = 3.141593 # if it is only % 9f, it indicates that the print length is 9 digits, and the decimal point is also one digit. If it is not enough, print spaces on the left side "PI = % 9f" % math. pi # output: PI = _ 3.141593 # only. if no subsequent number exists, it indicates that the fractional output integer is removed. 03 indicates that the value is not 3 digits, and 0 print "PI = % 03.f" % math is supplemented on the left. pi # output: PI = 003 # % 6.3f indicates that the number of digits after the decimal point is precise to three digits. The total length is 6 digits, including the decimal point. If the number is not enough, print "PI = % 6.3f" % math. pi # output: PI = _ 3.142 # %-6.3f indicates that the decimal point is precise to three digits, with a total length of six digits, including the decimal point, not enough space fill print "PI = %-6.3f" % math. pi # output: PI = 3.142 _ # % * can also be used *. * f is used to indicate the precision. The two * values are respectively specified in the first two digits of the parentheses # below. However, in this way, the 0 complement function is lost, only space allowed print "PI = % *. * f "% (06, 3, math. pi) # output: PI = _ 3.142
Summary

The above is all the details about formatting Python strings in % s % d % f. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.