Python basic formatting output, Python formatting output

Source: Internet
Author: User

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)

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.