免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2255 | 回复: 0
打印 上一主题 下一主题

Python Library Reference——String Formatting Op [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-02 10:57 |只看该作者 |倒序浏览

String and Unicode objects have one unique built-in operation: the % operator (modulo). This is also known as the string formatting or interpolation[改写] operator. Given format % values (where format is a string or Unicode object), % conversion specifications in format are replaced with zero or more elements of values. The effect is similar to the using sprintf() in the C language. If format is a Unicode object, or if any of the objects being converted using the %s conversion are Unicode objects, the result will also be a Unicode object.
If format requires a single argument, values may be a single non-tuple object(To format only a tuple you should therefore provide a singleton tuple whose only element is the tuple to be formatted).Otherwise, values must be a tuple with exactly the number of items specified by the format string, or a single mapping object (for example, a dictionary).
A conversion specifier contains two or more characters and has the following components, which must occur in this order:
  • The "%" character, which marks the start of the specifier.
  • Mapping key (optional), consisting of a parenthesised[圆括号] sequence of characters (for example, (somename)).
  • Conversion flags (optional), which affect the result of some conversion types.
  • Minimum field width (optional). If specified as an "*" (asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision.
  • Precision (optional), given as a "." (dot) followed by the precision. If specified as "*" (an asterisk), the actual width is read from the next element of the tuple in values, and the value to convert comes after the precision.
  • Length modifier (optional).
  • Conversion type.
    When the right argument is a dictionary (or other mapping type), then the formats in the string must include a parenthesised mapping key into that dictionary inserted immediately after the "%" character. The mapping key selects the value to be formatted from the mapping. For example:
    >>> print '%(language)s has %(#)03d quote types.' % \
              {'language': "Python", "#": 2}
    Python has 002 quote types.
    In this case no * specifiers may occur in a format (since they require a sequential parameter list).
    The conversion flag characters are:
    Flag
    Meaning
    #
    The value conversion will use the ``alternate form'' (where defined below).
    0
    The conversion will be zero padded for numeric values.
    -
    The converted value is left adjusted (overrides the "0" conversion if both are given).

    (a space) A blank should be left before a positive number (or empty string) produced by a signed conversion.
    +
    A sign character ("+" or "-") will precede the conversion (overrides a "space" flag).
    The length modifier may be h, l, and L may be present, but are ignored as they are not necessary for Python.
    The conversion types are:
    Conversion
    Meaning
    Notes
    d
    Signed integer decimal.
    i
    Signed integer decimal.
    o
    Unsigned octal.
    (1)
    u
    Unsigned decimal.
    x
    Unsigned hexadecimal (lowercase).
    (2)
    X
    Unsigned hexadecimal (uppercase).
    (2)
    e
    Floating point exponential format (lowercase).
    E
    Floating point exponential format (uppercase).
    f
    Floating point decimal format.
    F
    Floating point decimal format.
    g
    Same as "e" if exponent is greater than -4 or less than precision, "f" otherwise.
    G
    Same as "E" if exponent is greater than -4 or less than precision, "F" otherwise.
    c
    Single character (accepts integer or single character string).
    r
    String (converts any python object using repr()).
    (3)
    s
    String (converts any python object using str()).
    (4)
    %
    No argument is converted, results in a "%" character in the result.
    Notes:
    (1)
    The alternate form causes a leading zero ("0") to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.
    (2)
    The alternate form causes a leading '0x' or '0X' (depending on whether the "x" or "X" format was used) to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.
    (3)
    The %r conversion was added in Python 2.0.
    (4)
    If the object or format provided is a unicode string, the resulting string will also be unicode.
    Since Python strings have an explicit length, %s conversions do not assume that '\0' is the end of the string.
    For safety reasons, floating point precisions are clipped to 50; %f conversions for numbers whose absolute value is over 1e25 are replaced by %g conversions(These numbers are fairly arbitrary. They are intended to avoid printing endless strings of meaningless digits without hampering correct use and without having to know the exact precision of floating point values on a particular machine.). All other errors raise exceptions.
    Additional string operations are defined in standard modules
    string
    and
    re
    .


    本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8780/showart_149566.html
  • 您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

    北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年举报专区
    中国互联网协会会员  联系我们:huangweiwei@itpub.net
    感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP