Python formatted output
There are two ways in which Python formats output: percent and format
Format is more powerful than percent sign, where format alone can be custom character padding, string center display, conversion binary, Integer auto-segmentation, percent display and other functions are not compared with percent
1. Percent Semicolon 1.1 format
%[(name)][flags][width]. [Precision]typecode
1.2 Parameter description
- (name) optional, used to select the specified key
- Flags are optional, and the values to choose from are:
- + Right-aligned, positive plus, negative before plus minus;
- -left-justified; no sign before positive number, plus minus sign before negative number;
- Spaces are right-justified, plus a positive number before a negative number plus a minus sign;
- 0 Right alignment, no sign before positive number, minus sign before negative, and 0 padding in blank
- Width selectable, occupied widths
- . Precision optional, number of digits retained after the decimal point
- TypeCode must choose
- S, gets the return value of the __str__ method of the passed-in object and formats it to the specified location
- R, gets the return value of the __repr__ method of the passed-in object and formats it to the specified location
- C, Integer: Converts a number to its Unicode corresponding value, 10 binary range is 0 <= i <= 1114111 (py27 only supports 0-255); character: add character to specified position
- O, converts an integer to an octal representation and formats it to a specified location
- X, converts an integer to a hexadecimal representation and formats it to a specified location
- D, converts an integer, floating-point number to a decimal representation, and formats it to a specified position
- E, converting integers, floating-point numbers to scientific notation, and formatting them to a specified location (lowercase e)
- E, converting integers, floating-point numbers into scientific notation, and formatting them to a specified position (uppercase E)
- F, converts an integer, floating-point number to a floating-point number representation, and formats it to a specified position (the default is 6 digits after the decimal point)
- F, ibid.
- G, auto-adjust convert integers, floating-point numbers to floating-point or scientific notation (more than 6 digits in scientific notation), and format them to a specified location (if scientific counts are e;)
- G, auto-adjust convert integers, floating-point numbers to floating-point or scientific notation (more than 6 digits in scientific notation), and format them to a specified location (if scientific counts are e;)
- %, when there is a format flag in the string, a percent sign is required to denote a semicolon note: The percent sign format in Python does not have a way of automatically converting integers to binary representations
1.3 Common formatting
#!/usr/bin/env python#-*-coding:utf-8-*-#pyversion:python3.5#Owner:fuzjS1="I am%s, I am%d years old"% ('Jeck', 26)#output sequentially in position orderS2 ="I am% (name) s, I am% (age) d years old"% {'name':'Jeck',' Age': 26}#Custom Key OutputS3 ="I am% (name) +10s, I am% (age) d years old, I am% (height). 2f"% {'name':'Jeck',' Age': 26,'Height': 1.7512}#define a name width of 10 and align Right. defines height as a floating-point type, leaving the decimal point 2 bitsS4 ="original number:%d, octal:%o, 16 decimal:%x"% (15,15,15)#octal \ 16 binary ConversionS5 ="original number:%d, scientific counting method e:%e, scientific counting method e:%e"% (1000000000,1000000000,1000000000)#Scientific notation meansS6 ="percent display:%.2f%"% 0.75#percent semicolon indicatesPrint(S1)Print(S2)Print(S3)Print(S4)Print(S5)Print(S6)
Output Result:
I am Jeck, I am years oldi am years old, I am 1.7515, octal: 10 , 16 binary: F original number: 00000000, scientific counting method e:1.000000e+09, scientific counting method e:1.000000e+09 percent display:0.75
2.format Mode 2.1 Format:
[[Fill]align] [Sign] [#][0][width][,][.precision][type]
2.2 参数:
- Fill "optional" white space filled with characters
- Align "optional" alignment (required with width)
- <, content left justified
- Content right-aligned (default)
- =, the content is right-aligned, the symbol is placed to the left of the padding character, and only valid for numeric types. Even: symbol + filler + number
- , center content
- Sign "optional" with unsigned numbers
- +, positive home Plus, minus plus negative;
- -The plus sign does not change, minus is negative;
- Spaces, plus spaces, minus signs and negative;
- # "optional" for binary, octal, hex, if added #, 0b/0o/0x is displayed, otherwise not displayed
- , "optional" adds a delimiter to the number, such as: 1,000,000
- width "Optional" format the size of the placeholder
- . Precision "optional" decimal digits reserved Precision
- Type "optional" formatting types
- Parameters passed into the string type
- s, format string type data
- Blank, no type specified, default is None, same as S
- Parameters passed into the integer type
- B, automatic conversion of 10 binary integers to 2 binary representation and then formatting
- C, automatic conversion of 10 binary integers to their corresponding Unicode characters
- d, decimal integer
- O, the 10 binary integers are automatically converted to 8 binary representation and then formatted;
- X, automatically converts 10 binary integers to 16 binary representation and then formats (lowercase x)
- X, automatically converts 10 binary integers to 16 binary representation and then formats (uppercase X)
- Parameters passed in floating-point or decimal type
- E, converted to scientific notation (lowercase e), and then formatted;
- E, converted to scientific notation (capital E), and then formatted;
- F, converted to floating-point type (6 digits after the default decimal point), and then formatted;
- F, converted to floating-point type (6 digits after the default decimal point), and then formatted;
- G, automatically switch between E and F
- G, automatically switch between E and F
- %, display percent (default 6 digits after decimal)
2.3 Usage Examples
#!/usr/bin/env python#-*-coding:utf-8-*-#pyversion:python3.5#Owner:fuzjF1="I am {0}, I am {1}d years old". Format ('Jeck', 26)#use positional parameters to indexF2 ="I am {name}, I am {age}d years old". Format (**{'name':'Jeck',' Age': 26})#using a custom key to represent the k/v of the dictionaryF3 ="--{name:*^10s}--=={age:<10.2f}==". Format (name='Jeck', age=26.457)#The width of name is set to 10, the free use of the * number is not complete, and is centered, the age type is set to float, the width is 10. And left-justifiedF4 ="Original number: {:d} binary: {: b}, octal: {: o}, Hex x:{:x}, Hex x:{:x}". Format (15, 15, 15, 15, 15)#Binary ConversionF5 ="Original number: {:d}, scientific notation e:{:e}, scientific notation e:{:e}". Format (1000000000,1000000000,1000000000)#Scientific notation meansF6 ="Primitive: {: 2F}, percent semicolon denotes {:. 2%}, primitive: {:d}, auto-split: {:,}". Format (0.75,0.7584,10000000,10000000)#percent semicolon representation and auto-segmentationPrint(F1)Print(F2)Print(F3)Print(f4)Print(F5)Print(f6)
Output Result:
I am jeck, I am 26d years oldi am Jeck, I am 26d yearsold--***jeck***-- ==26.46 = = original number: binary : 1111, octal: +, hex x:f, hexadecimal x:f original number:1000000000, scientific notation e:1.000000e+09, scientific counting method e:1.000000e+09 original number:0.750000 , the percent semicolon represents 75.84%, the original number: 10000000, auto-segmentation means: 10,000,000
Python string formatted output