1, the output of integers
%o--oct octal
%d--dec Decimal
%x--hex Hex
1 >>> print ('%o '%) 2 243 >>> print ('%d ') 4 205 >>> print ('%x '% 20) 6 14
2, floating point output
(1) Formatted output
%f--retain six digits after the decimal point
%.3f, 3 decimal digits reserved
%e--reserved six digits after the decimal point, exponential output
%.3e, reserved 3 decimal digits, using scientific notation
%g--the use of a fractional method, otherwise using the scientific notation, while guaranteeing six digits of valid numbers
%.3G, retain 3 significant digits, using fractional or scientific notation
1 >>> print ('%f '% 1.11) # default reserved 6 decimal places 2 1.110000 3 >>> print ('%.1f '% 1.11) # take 1 decimal places 4 1.1 5 >&G t;> print ('%e '% 1.11) # Default 6 decimal places, with scientific notation 6 1.110000e+00 7 >>> print ('%.3e '% 1.11) # take 3 decimal places, use scientific notation 8 1.11 0e+00 9 >>> print ('%g '% 1111.1111) # Default 6-bit valid number 1111.1111 >>> print ('%.7g '% 1111.1111) # Fetch 7 bit valid Digital 1111.11113 >>> print ('%.2g '% 1111.1111) # takes 2 digits, automatically converts to scientific notation 1.1e+03
(2) built-in round ()
Round (number[, ndigits])
Parameters:
Number-This is a numeric expression.
Ndigits-Represents the number of digits to the last rounding. The default value is 0.
return value
The method returns the value after the decimal point of X is rounded to an n-digit number.
The round () function has only one parameter, does not specify the number of digits, returns an integer, and is the nearest integer, similar to rounding, when the number of decimal places for a trade-off is specified, the rule is also used for rounding. But when it comes to. 5, if the number of digits before the trade-off is odd, it is discarded directly, and if it is even, the upward trade-offs.
Note: ". 5" This is a "pit", and Python2 and python3 out of the interface is sometimes not the same, try to avoid using the round () function bar
1 >>> round (1.1125) # Rounding, not specifying the number of digits, rounding 2 1 3 >>> round (1.1135,3) # take 3 decimal places, because 3 is odd, then down "House" 4 1.113 5 &G T;>> round (1.1125,3) # takes 3 decimal places, because 2 is even, then up "into" 6 1.113 7 >>> round (1.5) # incomprehensible, Check out some of the information that Python would truncate the data, without probing 8 2 9 >>> round (2.5) # 211 >>> Round (1.675,2) # incomprehensible 12 1.681 3 >>> round (2.675,2)
3. String output
%s
%10s--right-aligned, placeholder 10-bit
%-10s--left-aligned, placeholder 10-bit
%.2s--intercept a 2-bit string
%10.2s--10 bit placeholder, intercept two-bit string
1 >>> print ('%s '% ' Hello World ') # string Output 2 Hello World 3 >>> print ('%20s '% ' Hello World ') # Right Align, take 20 bits, not enough then fill 4 Hello World 5 >>> print ('%-20s '% ' Hello World ') # Left Justified, take 20 bits, not enough then complement 6 Hello World 7 > >> print ('%.2s '% ' Hello World ') # take 2 bit 8 he 9 >>> print ('%10.2s '% ' Hello World ') # Right-aligned, take 2-bit h E11 >>> print ('%-10.2s '% ' Hello World ') # Left-Justified, take 2-bit he
4. Other
The string format code is as follows
(2) commonly used escape characters are as follows
Second, format usage
The relative basic format output uses the '% ' method, and the format () function is more powerful, the function takes a string as a template, formats it by passing in parameters, and uses curly braces ' {} ' as a special character instead of '% '
The method of use consists of two types: B.format (a) and format (A, b).
1. Basic usage
(1) without numbering, i.e. "{}"
(2) With numeric number, can be swapped in order, that is "{1}", "{2}"
(3) with keyword, "{a}", "{Tom}"
1 >>> print (' {} {} '. Format (' Hello ', ' World ') # without field 2 Hello World 3 >>> print (' {0} {1} '. Format (' Hel Lo ', ' World ') # with numeric number 4 Hello World 5 >>> print (' {0} {1} {0} '. Format (' Hello ', ' World ') # scrambled order 6 Hello Wor LD Hello 7 >>> print (' {1} {1} {0} '. Format (' Hello ', ' world ') 8 World World Hello 9 >>> print (' {a} {Tom} { A} '. Format (tom= ' Hello ', a= ' World ') # with keyword ten world Hello World
2. Advanced usage
(1) < (default) align Left, > right, ^ middle, = (numeric only) after decimal point
(2) Number of digits "{: 4s}", "{:. 2f}", etc.
1 >>> print (' {} and {} '. Format (' Hello ', ' World ') # default left justified 2 Hello and World 3 >>> print (' {: 10s} and { : >10s} '. Format (' Hello ', ' World ') # take 10-bit left-justified, take 10-bit right-aligned 4 Hello and World 5 >>> print (' {: ^10s} and {: ^10s} '. Format (' Hello ', ' World ') # take 10-bit middle alignment 6 Hello and world 7 >>> print (' {} is {:. 2f } '. Format (1.123,1.123) # takes 2 decimal places 8 1.123 is 1.12 9 >>> print (' {0} is {0:>10.2f} '. Format (1.123)) # take 2 bits Decimal, right-aligned, take 10-bit 1.123 is 1.12
3. Multiple formats
' B '-binary. The number is output as a base of 2. ' C '-character. Converts an integer to the corresponding Unicode string before printing. ' d '-decimal integer. The number is output as a base of 10. ' O '-eight binary. The number is output as a base of 8. ' x '-16 binary. The number is output in 16, and the number of digits above 9 is in lowercase letters. The ' e '-power symbol. Print numbers in scientific notation. Use ' e ' to indicate power. ' G '-general format. The value is output in fixed-point format. When the number is particularly large, it is printed in a power form. ' N '-number. When the value is an integer and ' d ', the values are the same as the ' G ' when they are floating-point numbers. The difference is that it inserts a numeric delimiter according to the locale. '% '-percent. Multiply the value by 100 and then print in fixed-point (' F ') format, followed by a percent semicolon.
1 >>> print (' {0:b} '. Format (3)) 2 3 >>> print (' {: c} '. Format (20)) 4
Python formatted output