Python Basic formatted output

Source: Internet
Author: User

What does formatted output mean?

The data is output according to some special requirements

If you enter an integer, you want the integer to follow the hexadecimal, octal output, if you enter a decimal number, you want the decimal to retain the 2 digits followed by the output, or a scientific notation to output decimals. The output of the string is expected to be output in 10 squares, or left-aligned, centered, etc.

Python string formatting symbols:

# about formatted output of integers

NUM01,NUM02 = 200,300

Print ("Octal output: 0o%o,0o%o"% (NUM01, num02))

Print ("Hex output: 0x%x,0x%x"% (NUM01, num02))

Print ("Decimal output:%d,%d"% (NUM01, num02))

Print ("200 binary output:", Bin (num01), "300 binary output as:", Bin (num02))

# floating point Output

%f retains six digits after the decimal point,%.3f retains three decimal places

%e retains six significant digits after the decimal point, which is output in exponential form. %.3E retains 3 decimal digits, using scientific notation

%g retains six digits of valid digits, using fractional notation, otherwise using scientific notation. %3G retain 3 digits, using fractional or scientific notation

num01=123456.8912

Print ("Standard mode:%f"%NUM01)

Print ("Hold two digits valid:%.2f"%NUM01)

Print ("E's Standard mode:%e"%NUM01)

Print ("Two valid digits for E:%.2e"%num01)

Print ("G's Standard mode:%g"%num01) #如果是7位保留不了就用科学计数法表示

Print ("Two valid digits for G:%.2g"%num01)

# formatted output of a string

%s Standard output

%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

str01= "www.iLync.cn"

Print ("s standard output:%s"% Str01)

Print ("s fixed-space output:%20s"% Str01) #右对齐

Print ("s fixed-space output:%-20s"% Str01) #左对齐

Print ("s intercept:%.3s"% Str01) #截取前三个字符

Print ("s intercept:%10.3s"% Str01)

Print ("s intercept:%-10.3s"% Str01)

Python Basic formatted output

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.