python3.9的输出操作

1、python3.9有更漂亮的输出格式

通常,你需要更多地控制输出的格式,而不仅仅是打印空格分隔的值。有几种格式化输出的方法。

  • 要使用 格式化字符串字面值 ,请在字符串的开始引号或三引号之前加上一个 f 或 F 。在此字符串中,你可以在 { 和 } 字符之间写可以引用的变量或字面值的 Python 表达式。
>>> year = 2016
>>> event = 'Referendum'
>>> f'Results of the {
      
      year} {
      
      event}'
'Results of the 2016 Referendum'
  • 字符串的 str.format() 方法需要更多的手动操作。你仍将使用 { 和 } 来标记变量将被替换的位置,并且可以提供详细的格式化指令,但你还需要提供要格式化的信息。
>>> yes_votes = 42_572_654
>>> no_votes = 43_132_495
>>> percentage = yes_votes / (yes_votes + no_votes)
>>> '{:-9} YES votes  {:2.2%}'.format(yes_votes, percentage)
' 42572654 YES votes  49.67%'
  • 最后,你可以使用字符串切片和连接操作自己完成所有的字符串处理,以创建你可以想象的任何布局。字符串类型有一些方法可以执行将字符串填充到给定列宽的有用操作。
    当你不需要花哨的输出而只是想快速显示某些变量以进行调试时,可以使用 repr() or str() 函数将任何值转化为字符串。

str() 函数是用于返回人类可读的值的表示,而 repr() 是用于生成解释器可读的表示(如果没有等效的语法,则会强制执行 SyntaxError)对于没有人类可读性的表示的对象, str() 将返回和 repr() 一样的值。很多值使用任一函数都具有相同的表示,比如数字或类似列表和字典的结构。特殊的是字符串有两个不同的表示。
几个例子:

>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'"
>>> str(1/7)
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
>>> print(s)
The value of x is 32.5, and y is 40000...
>>> # The repr() of a string adds string quotes and backslashes:
... hello = 'hello, world\n'
>>> hellos = repr(hello)
>>> print(hellos)
'hello, world\n'
>>> # The argument to repr() may be any Python object:
... repr((x, y, ('spam', 'eggs')))
"(32.5, 40000, ('spam', 'eggs'))"

string 模块包含一个 Template 类,它提供了另一种将值替换为字符串的方法,使用类似 $x 的占位符并用字典中的值替换它们,但对格式的控制要少的多。

格式化字符串文字

格式化字符串字面值 (常简称为 f-字符串)能让你在字符串前加上 f 和 F 并将表达式写成 {expression} 来在字符串中包含 Python 表达式的值。

可选的格式说明符可以跟在表达式后面。这样可以更好地控制值的格式化方式。以下示例将pi舍入到小数点后三位:

>>> import math
>>> print(f'The value of pi is approximately {
      
      math.pi:.3f}.')
The value of pi is approximately 3.142.

在 ‘:’ 后传递一个整数可以让该字段成为最小字符宽度。这在使列对齐时很有用。:

>>> table = {
    
    'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
>>> for name, phone in table.items():
...     print(f'{
      
      name:10} ==> {
      
      phone:10d}')
...
Sjoerd     ==>       4127
Jack       ==>       4098
Dcab       ==>       7678

其他的修饰符可用于在格式化之前转化值。 ‘!a’ 应用 ascii() ,‘!s’ 应用 str(),还有 ‘!r’ 应用 repr():

>>> animals = 'eels'
>>> print(f'My hovercraft is full of {
      
      animals}.')
My hovercraft is full of eels.
>>> print(f'My hovercraft is full of {
      
      animals!r}.')
My hovercraft is full of 'eels'.

字符串的 format() 方法

str.format() 方法的基本用法如下所示:

>>> print('We are the {} who say "{}!"'.format('knights', 'Ni'))
We are the knights who say "Ni!"

花括号和其中的字符(称为格式字段)将替换为传递给 str.format() 方法的对象。花括号中的数字可用来表示传递给 str.format() 方法的对象的位置。

>>> print('{0} and {1}'.format('spam', 'eggs'))
spam and eggs
>>> print('{1} and {0}'.format('spam', 'eggs'))
eggs and spam

如果在 str.format() 方法中使用关键字参数,则使用参数的名称引用它们的值。:

>>> print('This {food} is {adjective}.'.format(
...       food='spam', adjective='absolutely horrible'))
This spam is absolutely horrible.

位置和关键字参数可以任意组合:

>>> print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
                                                       other='Georg'))
The story of Bill, Manfred, and Georg.

如果你有一个非常长的格式字符串,你不想把它拆开,那么你最好是按名称而不是按位置引用变量来进行格式化。 这可以通过简单地传递字典并使用方括号 ‘[]’ 访问键来完成。

>>> table = {
    
    'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '
...       'Dcab: {0[Dcab]:d}'.format(table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678

这也可以通过使用 ‘**’ 符号将 table 作为关键字参数传递。

>>> table = {
    
    'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678

这在与内置函数 vars() 结合使用时非常有用,它会返回包含所有局部变量的字典。
例如,下面几行代码生成一组整齐的列,其中包含给定的整数和它的平方以及立方:

>>> for x in range(1, 11):
...     print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))
...
 1   1    1
 2   4    8
 3   9   27
 4  16   64
 5  25  125
 6  36  216
 7  49  343
 8  64  512
 9  81  729
10 100 1000

手动格式化字符串

这是同一个平方和立方的表,手动格式化的:

>>> for x in range(1, 11):
...     print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ')
...     # Note use of 'end' on previous line
...     print(repr(x*x*x).rjust(4))
...
 1   1    1
 2   4    8
 3   9   27
 4  16   64
 5  25  125
 6  36  216
 7  49  343
 8  64  512
 9  81  729
10 100 1000

(注意每列之间的一个空格是通过使用 print() 的方式添加的:它总是在其参数间添加空格。)

字符串对象的 str.rjust() 方法通过在左侧填充空格来对给定宽度的字段中的字符串进行右对齐。类似的方法还有 str.ljust() 和 str.center() 。这些方法不会写入任何东西,它们只是返回一个新的字符串,如果输入的字符串太长,它们不会截断字符串,而是原样返回;这虽然会弄乱你的列布局,但这通常比另一种方法好,后者会在显示值时可能不准确(如果你真的想截断,你可以添加一个切片操作,例如 x.ljust(n)[:n] 。)

还有另外一个方法,str.zfill() ,它会在数字字符串的左边填充零。它能识别正负号:

>>> '12'.zfill(5)
'00012'
>>> '-3.14'.zfill(7)
'-003.14'
>>> '3.14159265359'.zfill(5)
'3.14159265359'

旧的字符串格式化方法

% 运算符(求余)也可用于字符串格式化。 给定 ‘string’ % values,则 string 中的 % 实例会以零个或多个 values 元素替换。 此操作通常被称为字符串插值。 例如:

>>> import math
>>> print('The value of pi is approximately %5.3f.' % math.pi)
The value of pi is approximately 3.142.

猜你喜欢

转载自blog.csdn.net/javascript_good/article/details/132752525
今日推荐