format to format the output in Python, more powerful tool output

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45523154/article/details/102730826

What is the format to format the output?

The basic format output with respect to the use of "%" method, format powerful, which function as a template string, formatted parameters passed, and braces "{}" instead of as a special character "% ", which is somewhat similar to C # placeholder

formatted output format

Basic usage 1) format of

  • Not numbered, i.e., "{}"
  • Numbered, the order may be reversed, i.e., "{1}", "{2}"
  • With the keyword, i.e., "{a}", "{tom}"

Such as:

The basic format of usage

2) format Advanced use

  • <(Default) Left,> right alignment, center alignment ^
  • Bits take "{: 4s}", "{:. 2f}", etc.

Such as:

![(https://upload-images.jianshu.io/upload_images/18829896-e746d3f5f6c62300?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

Advanced use format

3) format usage value

  • "B" binary radix number to two outputs
  • "C" character, before printing the integer conversion to the corresponding unicode string
  • "D" decimal number, and outputs a digital base 10
  • "O" octal, base 8 digital outputs
  • "X" hexadecimal number, the digital output base 16, represented by the above 9 af
  • "E" the power notation, the digital printing scientific notation. With "e" represents power
  • "G" general format, the digital output to fixed-point, when the value particularly when the power in the form of print
  • "N" number, when the value of the integer and "d" the same, a floating point value and "g" the same, except that he will insert digital Separator according to locale.
  • "%" Percentage value is multiplied by 100 to print, retention six decimal places, the value will be behind a percent sign
  • "," Thousand separators, large data value or currency often used

Interested in Python or are studying a small partner, you can join us to learn Python buckle qun: 784758214, look at how seniors are learning! From basic web development python script to, reptiles, django, data mining and other projects to combat zero-based data are finishing. Given to every little python partner! Every day, Daniel explain the timing Python technology, to share some of the ways to learn and need to pay attention to small details, click on Join us python learner gathering

Such as:

Guess you like

Origin blog.csdn.net/weixin_45523154/article/details/102730826