About format formatting output and format formatting

Source: Internet
Author: User

About format formatting output and format formatting

What isFormat?

Compared with the basic formatting output, the "%" method is used. The format function is powerful. This function uses the string as a template and uses the input parameters for formatting, the braces "{}" are used as special characters instead of "%", which is similar to the placeholder in C #.

1)FormatBasic usage

No ID, that is, "{}"

With a number, you can change the order, that is, "{1}", "{2 }"

With the keyword "{a}", "{tom }"

 

For example:

Name = "Sandy"

Gender = "female"

Age = 18

Print ("name: % s, gender: % s age: % d" % (name, gender, age ))

Print ("name :{}, gender :{} age :{}". format (name, gender, age ))

# You can call it multiple times with a number.

Print ("name: {0}, gender: {1} age: {2} Student name: {0}". format (name, gender, age ))

# Easy to understand the ID name

Print ("name: {name}, gender: {gender} age: {age} Student name: {name }". format (name = name, gender = gender, age = age ))

 

Demo:

 

 

2) Advanced usage of format

<(Default) left alignment,> right alignment, ^ center alignment

Number of digits: "{: 4 s}", "{:. 2f}", etc.

 

For example:

Print ("name: {: 10}". format (name) # left aligned by default

Print ("name: {: <10}". format (name) # Left-aligned logo

Print ("name: {:> 10}". format (name) # right alignment

Print ("name: {: ^ 10}". format (name) # center alignment

Print ("{:. 2f}". format (3.1415926) # retain two valid digits

Print ("{: 10.2f}". format (3.1415926) # retain two valid digits, which are right aligned by default.

Print ("{:> 10.2f}". format (3.1415926) # retain two valid digits to indicate the right alignment

Print ("{: <10.2f}". format (3.1415926) # retain two valid digits to indicate the left alignment

Print ("{: ^ 10.2f}". format (3.1415926) # Keep two valid digits in the middle of the alignment.

Demo:

 

3)FormatValue

  • "B" binary. The number is output in the base of two digits.
  • "C" character, convert the integer to the corresponding unicode string before printing
  • "D" decimal number. The number is output based on 10.
  • "O" Octal numbers, which are output based on 8
  • "X" hexadecimal number. The number is output based on 16. a-f is used to represent more than 9 digits.
  • "E" power symbol, which is used to print numbers in scientific notation. Use "e" to represent the power
  • "G" is in the general format. Numbers are output in fixed-point format. When the value is very large, the numbers are printed in the power form.
  • "N" number. If the value is an integer, it is the same as "d". If the value is a floating point, it is the same as "g". The difference is that it inserts a digital separator according to the region settings.
  • "%" Percent, numeric value multiplied by 100 print, retain 6 decimal places, the value will be followed by a percent sign
  • "," Thousands separator, often used for currency representation or big data values

 

For example:

Num01, num02 = 200,300

Print ("hexadecimal print: {0: x} {1: x}". format (num01, num02 ))

Print (": {0: o} {1: o}". format (num01, num02 ))

Print ("binary print: {0: B} {1: B}". format (num01, num02 ))

Print ("{0: c}". format (76) # encode a specific character. For more information, see ASCll

Print ("{: e}". format (123456.77544) # retain 6 digits after the decimal point by default

Print ("{: 0.2e}". format (123456.77544) # retain two digits after the decimal point

Print ("{: g}". format (123456.77544) # retain 6 digits

Print ("{: g}". format (123456789.77544) # more than 6 digits are represented by scientific notation

Print ("{: %}". format (34) # retain 6 digits after the decimal point by default

Print ("{: 0.2%}". format (34 ))

Print ("{:,}". format (1234567890 ))

 

Demo:

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.