Python的字符串格式化

旧的格式化字符串方法

通过操作符 % ,以类似 sprintf()-style 的方式解析左参数,将右参数应用于此,得到格式化操作生成的字符串,例如:

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

因为 str.format() 还很新,大量 Python 代码还在使用 % 操作符。然而,因为旧式的格式化方法最终将从语言中去掉,应该尽量使用 str.format() 。


新的格式化字符串方法

在Python 3.0中,%操作符通过一个更强的格式化方法format()进行了增强。对str.format()的支持已经被反向移植到了Python 2.6

方法 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.

'!a' (应用 ascii()), '!s' (应用 str() ) 和 '!r' (应用 repr() ) 可以在格式化之前转换值:

>>> import math
>>> print('The value of PI is approximately {}.'.format(math.pi))
The value of PI is approximately 3.14159265359.
>>> print('The value of PI is approximately {!r}.'.format(math.pi))
The value of PI is approximately 3.141592653589793.

字段名后允许可选的 ':' 和格式指令。这允许对值的格式化加以更深入的控制。下例将 Pi 转为三位精度。

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

在字段后的 ':' 后面加一个整数会限定该字段的最小宽度,这在美化表格时很有用。

注意:下面的示例中,由于dict的存储不是按照list的方式顺序排列,所以,迭代出的结果顺序不一样

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

如果你有个实在是很长的格式化字符串,不想分割它。如果你可以用命名来引用被格式化的变量而不是位置就好了。有个简单的方法,可以传入一个字典,用中括号访问它的键

>>> 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 = {'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() 组合使用非常有效。该函数返回包含所有局部变量的字典。

类和类型可以定义一个__format__()方法来控制怎样格式化自己,还有一个内置的format()方法可以用来格式化一个值。

它会调用类型的__format__()方法,并将格式化指示符作为参数传进去。
>>> format(75.6564, '.2f')
 '75.66'

关于format的更深入的用法,请参见PEP 3101,这里节选个人最常用的一部分:

Standard Format Specifiers

    If an object does not define its own format specifiers, a standard
    set of format specifiers is used.  These are similar in concept to
    the format specifiers used by the existing '%' operator, however
    there are also a number of differences.

    The general form of a standard format specifier is:

        [[fill]align][sign][#][0][minimumwidth][.precision][type]

    The brackets ([]) indicate an optional element.

    Then the optional align flag can be one of the following:

        '<' - Forces the field to be left-aligned within the available
              space (This is the default.)
        '>' - Forces the field to be right-aligned within the
              available space.
        '=' - Forces the padding to be placed after the sign (if any)
              but before the digits.  This is used for printing fields
              in the form '+000000120'. This alignment option is only
              valid for numeric types.
        '^' - Forces the field to be centered within the available
              space.

    Note that unless a minimum field width is defined, the field
    width will always be the same size as the data to fill it, so
    that the alignment option has no meaning in this case.

    The optional 'fill' character defines the character to be used to
    pad the field to the minimum width.  The fill character, if present,
    must be followed by an alignment flag.

    The 'sign' option is only valid for numeric types, and can be one
    of the following:

        '+'  - indicates that a sign should be used for both
               positive as well as negative numbers
        '-'  - indicates that a sign should be used only for negative
               numbers (this is the default behavior)
        ' '  - indicates that a leading space should be used on
               positive numbers

    If the '#' character is present, integers use the 'alternate form'
    for formatting.  This means that binary, octal, and hexadecimal
    output will be prefixed with '0b', '0o', and '0x', respectively.

    'width' is a decimal integer defining the minimum field width.  If
    not specified, then the field width will be determined by the
    content.
    
    If the width field is preceded by a zero ('0') character, this enables
    zero-padding.  This is equivalent to an alignment type of '=' and a
    fill character of '0'.

    The 'precision' is a decimal number indicating how many digits
    should be displayed after the decimal point in a floating point
    conversion.  For non-numeric types the field indicates the maximum
    field size - in other words, how many characters will be used from
    the field content.  The precision is ignored for integer conversions.

    Finally, the 'type' determines how the data should be presented.
    
    The available integer presentation types are:

        'b' - Binary. Outputs the number in base 2.
        'c' - Character. Converts the integer to the corresponding
              Unicode character before printing.
        'd' - Decimal Integer. Outputs the number in base 10.
        'o' - Octal format. Outputs the number in base 8.
        'x' - Hex format. Outputs the number in base 16, using lower-
              case letters for the digits above 9.
        'X' - Hex format. Outputs the number in base 16, using upper-
              case letters for the digits above 9.
        'n' - Number. This is the same as 'd', except that it uses the
              current locale setting to insert the appropriate
              number separator characters.
        '' (None) - the same as 'd'

    The available floating point presentation types are:

        'e' - Exponent notation. Prints the number in scientific
              notation using the letter 'e' to indicate the exponent.
        'E' - Exponent notation. Same as 'e' except it converts the
              number to uppercase.
        'f' - Fixed point. Displays the number as a fixed-point
              number.
        'F' - Fixed point. Same as 'f' except it converts the number
              to uppercase.
        'g' - General format. This prints the number as a fixed-point
              number, unless the number is too large, in which case
              it switches to 'e' exponent notation.
        'G' - General format. Same as 'g' except switches to 'E'
              if the number gets to large.
        'n' - Number. This is the same as 'g', except that it uses the
              current locale setting to insert the appropriate
              number separator characters.
        '%' - Percentage. Multiplies the number by 100 and displays
              in fixed ('f') format, followed by a percent sign.
        '' (None) - similar to 'g', except that it prints at least one
              digit after the decimal point.

    Objects are able to define their own format specifiers to
    replace the standard ones.  An example is the 'datetime' class,
    whose format specifiers might look something like the
    arguments to the strftime() function:

        "Today is: {0:%a %b %d %H:%M:%S %Y}".format(datetime.now())

    For all built-in types, an empty format specification will produce
    the equivalent of str(value).  It is recommended that objects
    defining their own format specifiers follow this convention as
    well.


你可能感兴趣的:(Python学习笔记)