Python Data Format: % s string, % d integer, % f floating point, python % f

Source: Internet
Author: User

Python Data Format: % s string, % d integer, % f floating point, python % f

Formatting character %
% Percent mark # Is to output a %
% C 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 (hexadecimal uppercase)
% E floating point number (Scientific Notation)
% E floating point number (Scientific notation, replaced by e)
% F floating point number (decimal point)
% G floating point number (% e or % f based on the value)
% G floating point number (similar to % g)
% P pointer (memory address for printing value in hexadecimal format)
% N stores the number of output characters in the next variable in the parameter list

% 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

Practical

# Generate a 9*9 multiplication table print ('\ n '. join ([''. join (["% d * % d = % 2 s" % (y, x, x * y) for y in range (1, x + 1)]) for x in range (1, 10)])

 

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.