Python Formatted Output Details
- 2021-12-04 10:28:50
- OfStack
1.% Formatting
Syntax:
%[(name)][flags][width].[precision]typecode
Parameters:
(name) Optional to select the specified key
flags optional, alignment, optional values:
+ means right alignment
-Indicates left alignment.
'' is a space, which means that a space is filled to the left of a positive number and a negative number is preceded by a minus sign.
Align 0 to the right, precede positive numbers with unsigned, precede negative numbers with negative signs, and fill in the blank with 0
width optional for display width
. precision optionally represents the number of decimal places
Required for typecode. The parameters you can select are:
s represents a string
d represents an integer
f denotes floating-point numbers
1.1 Simple formatting without optional parameters
print('%s The total assets of %f Yuan '%('A Company ',156261595.89))
print('%s The total assets of %d Yuan '%('A Company ',156261595.89))
Implementation results:
The total assets of A Company are 156,261,595.890, 000 Yuan
The total assets of A Company are 156,261,595 yuan
1.2 Complex formatting, using optional parameters
1.2. 1 Parameter (name) to select the specified key
d = {'name':'A Company ', 'assets':156261595.89}
print(" Company name %(name)s, Total assets are %(assets)f Yuan. " %d)
Implementation results:
The name of the company is A Company, and the total assets are 156,261,595.890, 000 yuan.
1.2. 2 Parameters flags and width, Alignment and Width
d = {'name':'A Company ', 'assets':156261595.89}
print(" Company name %(name)+6s, Total assets are %(assets)-20f Yuan. " %d)
Implementation results:
The name of the company is A Company, and the total assets are 156,261,595.890, 000 yuan.
1.2. 3 Parameters. precision controls the number of decimal places
d = {'name':'A Company ', 'assets':156261595.89345}
print(" Company name %(name)+4s, Total assets are %(assets).2f Yuan. " %d)
Implementation results:
Company name A Company, with total assets of 156,261,595.89 yuan.
2. format formatting
Functions that format strings
str.format(),
The basic syntax is through{}
And:
To implement formatting instead of the% formatting method,format
Functions can accept unlimited arguments, and their positions can be out of order.
Interpretation of main parameters:
[Padding]: Character with padding after the number, only 1 character. If not specified, it will be filled with space by default
[Alignment and Width] ^, < , > They are centered, left-aligned, right-aligned, with width at the back
[Positive and negative number display] + means display + before positive number and display-before negative number; (Space) Indicates putting a space before a positive number
[Data Type] s for String d for Integer f for Floating Point Number
2.1 Custom placeholders
# Custom placeholder
s = '{0} The total assets of {1} Yuan '
print(s.format('A Company ','156261595.89'))
Implementation results:
The total assets of A Company are 156,261,595.89 yuan
2.2 Custom Keywords
# Custom keywords
s = '{name} The total assets of { Assets } Yuan '
print(s.format(name='A Company ', Assets ='156261595.89'))
Implementation results:
The total assets of A Company are 156,261,595.89 yuan
2.3 Fill and Align (both)
# Fill and align (both)
# : The character with padding after the number can only be 1 Characters. If not specified, it will be filled with spaces by default
# ^, <, > They are centered, left-aligned, right-aligned, with width at the back.
s = '{name} The total assets of { Assets :*>20} Yuan '
print(s.format(name='A Company ', Assets =156261595.89))
Implementation results:
The total assets of A Company are ********* 156261595.89 yuan
2.4 Numeric Formatting
# Floating point type, preserving two decimal places
s = '{name} The total assets of { Assets :.2f} Yuan '
print(s.format(name='A Company ', Assets =156261595.8988))
Implementation results:
The total assets of A Company are 156,261,595.90 yuan
# Floating point type, preserving two decimal places and using thousandth separator
s = '{name} The total assets of { Assets :,.2f} Yuan '
print(s.format(name='A Company ', Assets =156261595))
Implementation results:
The total assets of A Company are 156,261,595.00 Yuan
# Floating point type, preserving two decimal places and using thousandth separator , Indicates that the display is preceded by a positive number + Shows before negative numbers -
s = '{name} The total assets of { Assets :+,.2f} Yuan '
print(s.format(name='A Company ', Assets =156261595))
Implementation results:
The total assets of A Company are +156,261,595.00 Yuan
d = {'name':'A Company ', 'assets':156261595.89}
print(" Company name %(name)s, Total assets are %(assets)f Yuan. " %d)
0
Implementation results:
The asset-liability ratio of A Company is 85.44%
3. f-String formatting
Python
Formatting string
f-string
, using
f'{content:format}'
Formatting a string where
content
Is to replace and fill in the contents of a string, which can be variables, expressions, functions, etc. You do not have to specify when using the default format
{:format
}, just write {
str.format(),
0
}.
3.1 Easy to use
d = {'name':'A Company ', 'assets':156261595.89}
print(" Company name %(name)s, Total assets are %(assets)f Yuan. " %d)
1
Implementation results:
The total assets of A Company are 156,261,595 yuan.
3.2 Complex Control
d = {'name':'A Company ', 'assets':156261595.89}
print(" Company name %(name)s, Total assets are %(assets)f Yuan. " %d)
2
Implementation results:
The total assets of A Company are 156,261,595.00 yuan.
Note: Filling and alignment and processing of numeric values are the same as those of
format
Function formatting is similar, refer to
format
Part of the explanation.