Formatted output of Python variables

The print() function uses conversion specifiers beginning with% to format and output various types of data.

The Conversion Specifier is just a placeholder (also called a formatting operator), which will be replaced by the value of the following expressions (variables, constants, numbers, strings, addition, subtraction, multiplication, and division, etc.).

Conversion specifier Explanation
%d、%i Convert to signed decimal integer
%The Convert to signed octal integer
%x、%X Convert to signed hexadecimal integer
%e Converted to a floating point number in scientific notation (e lowercase)
%E Converted to a floating point number in scientific notation (E capital)
%f、%F Convert to decimal floating point number
%g Smart choice to use %f or %e format
%G Smart choice to use %F or %E format
%c Formatting characters and their ASCII codes
%r Use the repr() function to convert the expression to a string
%s Use the str() function to convert the expression to a string
%% Output%

Format syntax format

print("格式化字符串" % 变量1)
print("格式化字符串" % (变量1, 变量2...))

In the print() function, the formatted string surrounded by quotation marks is equivalent to a string template in which some conversion specifiers (placeholders) can be placed.

The% in the middle is a separator. The format string is in front of it, and the expression to be output is behind it.

Of course, the format string can also contain multiple conversion specifiers. At this time, multiple expressions must be provided to replace the corresponding conversion specifiers. Multiple expressions must be surrounded by parentheses ().

For example

name = '小明'
print("我的名字叫 %s,请多多关照!" % name) 
# 我的名字叫 小明,请多多关照!

student_no = 1
print("我的学号是 %06d" % student_no)
# 我的学号是 000001

scale = 0.2
print("数据比例是 %.02f%%" % (scale * 100))
# 数据比例是 20.00%

Specify the minimum output width

When using the conversion specifier, you can use the following format to specify the minimum output width (at least the number of characters occupied):

  • %10d means that the output integer width is at least 10
  • %20s means the output string width is at least 20
n = 1234567
print("n(10):%10d." % n)
print("n(5):%5d." % n)
url = "https://www.baidu.com/python/"
print("url(35):%35s." % url)
print("url(20):%20s." % url)

The execution result is:

n(10):   1234567.
n(5):1234567.
url(35):      https://www.baidu.com/python/.
url(20):https://www.baidu.com/python/.

From the execution results, it can be found that for integers and strings, when the actual width of the data is less than the specified width, it will be filled with spaces on the left; when the actual width of the data is greater than the specified width, it will be output according to the actual width of the data.

Specify alignment

By default, the data output by print() is always right-aligned.

When the data is not wide enough, the data is always output to the right, and spaces are added to the left to reach the specified width.

Python allows adding a flag before the minimum width to change the alignment. The flags supported by Python are as follows.

Sign Description
- Specify left alignment
+ Indicates that the output number always has a sign; integers with +, negative numbers with -.
0 Indicates that 0 is added when the width is insufficient, rather than a space.

A few notes:

  • For integers, when left justification is specified, adding 0 to the right has no effect, because this will change the value of the integer.
  • For decimals, the above three signs can exist at the same time.
  • For character strings, only the-flag can be used, because the symbol has no meaning for character strings, and adding 0 will change the value of the character string.
    example:
n = 123456
# %09d 表示最小宽度为9,左边补0
print("n(09):%09d" % n)
# %+9d 表示最小宽度为9,带上符号
print("n(+9):%+9d" % n)
f = 140.5
# %-+010f 表示最小宽度为10,左对齐,带上符号
print("f(-+0):%-+010f" % f)
s = "Hello"
# %-10s 表示最小宽度为10,左对齐
print("s(-10):%-10s." % s)

Results of the

n(09):000123456
n(+9):  +123456
f(-+0):+140.500000
s(-10):Hello     .

Specified decimal precision

For decimals (floating point numbers), print() also allows you to specify the number of digits after the decimal point, that is, specify the output precision of the decimal.

The precision value needs to be placed after the minimum width, separated by a dot.; it is also possible not to write the minimum width and only the precision. The specific format is as follows:

%m.nf
%.nf

m represents the minimum width, n represents the output precision, and. must exist.

Please look at the following code:

f = 3.141592653
# 最小宽度为8,小数点后保留3位
print("%8.3f" % f)
# 最小宽度为8,小数点后保留3位,左边补0
print("%08.3f" % f)
# 最小宽度为8,小数点后保留3位,左边补0,带符号
print("%+08.3f" % f)

operation result:

   3.142
0003.142
+003.142

Insert picture description here

Guess you like

Origin blog.csdn.net/beyondamos/article/details/107755933