1.% Formatting
Grammar:
%[(name)][flags][width].[precision]typecode
Parameters:
(name) Optional, for selecting the specified key
flags Optional, alignment, optional value is:
+ means right-aligned
-indicates left alignment.
' ' is a space, indicating that a space is filled in to the left of a positive number and a negative number is preceded by a minus sign.
0 Right-aligned, positive numbers preceded by no sign, negative numbers preceded by a minus sign, blanks filled with zeros
width Optional Indicates display width
.precision Optional Indicates the number of decimal places
typecode Required , the parameters that can be selected are:
s denotes a string
d Represents an integer
f denotes a floating point number
1.1 Simple formatting without optional parameters
print('%s total assets are %f dollars'%('Company A',156261595.89)) print('%s total assets are %d$'%('Company A',156261595.89))
Implementation results:
The total assets of Company A are $15,626,1595.890000
Company A has total assets of $15,626,595
1.2 Complex formatting, using optional parameters
1.2.1 Parameter (name), used to select the specified key
d = {'name':'Company A', 'assets':156261595.89} print("company identification%(name)s,Total assets amounted to%(assets)funit of money (in PRC: Chinese yuan, in USA: dollar, etc)。" %d)
Implementation results:
The name of the company is Company A. The total assets are $15,626,1595.890000.
1.2.2 Parameters flags and width, alignment and width
d = {'name':'Company A', 'assets':156261595.89} print("company identification%(name)+6s,Total assets amounted to%(assets)-20funit of money (in PRC: Chinese yuan, in USA: dollar, etc)。" %d)
Implementation results:
The name of the company is Company A, with total assets of $15,626,1595.890000.
1.2.3 The .precision parameter controls the number of decimal places.
d = {'name':'Company A', 'assets':156261595.89345} print("company identification%(name)+4s,Total assets amounted to%(assets).2funit of money (in PRC: Chinese yuan, in USA: dollar, etc)。" %d)
Implementation results:
Name of the company Company A, with total assets of $15,626,595.89.
formatting
Functions to format strings
(),
The basic syntax is passed through the{}
cap (a poem):
to implement formatting in place of the %format method, theformat
Functions can take an unlimited number of arguments, and the positions can be out of order.Main Parameter Interpretation:
[Filling]The character to be padded after the : sign can only be one character, if not specified, the default is padded with spaces.
[Alignment and width]^, <, > are centered, left-justified, and right-justified, respectively, followed by a width
[Positive and negative numbers displayed] + indicates that + is displayed before a positive number and - before a negative number; (space) indicates that a space is added before a positive number
[Data type] s for strings d for integers f for floating point numbers
2.1 Customizing Placeholders
# Custom placeholders s = '{0}The total assets of the{1}unit of money (in PRC: Chinese yuan, in USA: dollar, etc)' print(('Company A','156261595.89'))
Implementation results:
Company A has total assets of $15,626,595.89
2.2 Customized keywords
# Customized keywords s = '{name}The total assets of the{liabilities}unit of money (in PRC: Chinese yuan, in USA: dollar, etc)' print((name='Company A',liabilities='156261595.89'))
Implementation results:
Company A has total assets of $15,626,595.89
2.3 Filling and alignment (used together)
# Filling and alignment (used together) # The :sign is followed by a padded character, which can only be a single character; if you don't specify it, it is padded with spaces by default. # ^, <, > are centered, left-justified, and right-justified, respectively, followed by the width of the s = '{name}The total assets of the{liabilities:*>20}unit of money (in PRC: Chinese yuan, in USA: dollar, etc)' print((name='Company A',liabilities=156261595.89))
Implementation results:
The total assets of Company A are ********156261595.89
2.4 Digital formatting
# Floating point, two decimal places s = '{name}The total assets of the{liabilities:.2f}unit of money (in PRC: Chinese yuan, in USA: dollar, etc)' print((name='Company A',liabilities=156261595.8988))
Implementation results:
Company A has total assets of $15,626,595.90
# Floating-point with two decimal places and thousands separator s = '{name}The total assets of the{liabilities:,.2f}unit of money (in PRC: Chinese yuan, in USA: dollar, etc)' print((name='Company A',liabilities=156261595))
Results.
The total assets of Company A are $156,261,595.00
# Floating-point, with two decimal places and a thousandths separator, indicating that + is displayed before positive numbers and - before negative numbers. s = '{name}The total assets of the{liabilities:+,.2f}unit of money (in PRC: Chinese yuan, in USA: dollar, etc)' print((name='Company A',liabilities=156261595))
Results.
Company A's total assets are +$156,261,595.00
# Percentage, two decimal places s = '{name}The gearing ratio is{gearing:.2%}' print((name='Company A',gearing=0.8544))
Results.
Company A has a gearing ratio of 85.44%
-String formatting
Python
Formatting stringsf-string
, usingf'{content:format}'
Sets the string format, wherecontent
is to replace and fill in the contents of the string, which can be a variable, expression, function, etc. You don't need to specify the{:format
}, just write {content
} that is.
3.1 Simple use
# Simple to use name = 'Company A' assets = 156261595 msg = f'{name}The total assets of the{assets}unit of money (in PRC: Chinese yuan, in USA: dollar, etc)。' print(msg)
Results.
Company A has total assets of $15,626,595.
3.2 Complex control
# {content:format} Formatting using the name = 'Company A' assets = 156261595 msg = f'{name}The total assets of the{assets:,.2f}unit of money (in PRC: Chinese yuan, in USA: dollar, etc)。' print(msg)
Results.
Company A has total assets of $156,261,595.00.
Annotation:Filling and alignment and the handling of numeric values with theformat
The function formatting is similar, cf.format
Part of the explanation.
to this article on Python formatting output details of the article is introduced to this, more related Python formatting output content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!