Python basic formatting output, Python formatting output
What is formatted output?
Data is output according to certain special requirements
Assume that an integer is input, and an integer is expected to be output in octal notation. If a decimal number is input, it is expected that the second decimal number is retained and then output, or the decimal number is output in scientific notation. The string output is expected to be output in ten grids, or left aligned, centered, and so on.
Python string formatting symbol:
#About integer formatting output
Num01, num02 = 200,300
Print (": 0o % o, 0o % o" % (num01, num02 ))
Print ("hexadecimal output: 0x % x, 0x % x" % (num01, num02 ))
Print ("decimal output: % d, % d" % (num01, num02 ))
Print ("200 binary output:", bin (num01), "300 binary output:", bin (num02 ))
#Floating Point Output
% F keep the six digits after the decimal point, and %. 3f keep the three decimal places
% E retain the six valid digits after the decimal point and output them in exponential form. %. 3e keeps three decimal places and uses scientific notation
% G indicates that the decimal number is used when the six valid digits are retained. Otherwise, the scientific notation is used. % 3G retain three valid digits, using decimals or scientific notation
Num01= 123456.8912
Print ("standard mode: % f" % num01)
Print ("keep two valid numbers: %. 2f" % num01)
Print ("e standard mode: % e" % num01)
Print ("Leave two valid numbers for e: %. 2e" % num01)
Print ("g standard mode: % g" % num01) # use scientific notation if the value is 7 bits.
Print ("Leave two valid numbers for g: %. 2g" % num01)
#Formatted string output
% S standard output
% 10 s right alignment, 10 placeholders
% 10 s left alignment, 10 placeholders
%. 2 s truncated 2-character string
% 10.2 s 10-digit placeholder, intercepting two strings
Str01 = "www.iLync.cn"
Print ("s standard output: % s" % str01)
Print ("s fixed space output: % 20 s" % str01) # right alignment
Print ("s fixed space output: %-20 s" % str01) # Left alignment
Print ("s truncation: %. 3 s" % str01) # capture the first three characters
Print ("s interception: % 10.3 s" % str01)
Print ("s truncation: %-10.3 s" % str01)