python 格式化字符串(官方文档)
2016-07-18 16:01
363 查看
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,
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:
In this case no
The conversion flag characters are:
A length modifier (
may be present, but is ignored as it is not necessary for Python.
The conversion types are:
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
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
the end of the string.
For safety reasons, floating point precisions are clipped to 50;
by
other errors raise exceptions.
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). |
h,
l, or
L)
may be present, but is ignored as it is not necessary for Python.
The conversion types are:
Conversion | Meaning | Notes |
---|---|---|
d | 有符号整数 | |
i | 有符号整数 | |
o | 无符号八进制 | (1) |
u | 无符号十进制 | |
x | 无符号十六进制(小写). | (2) |
X | 无符号十六进制(大写). | (2) |
e | 浮点数指数格式(小写). | |
E | 浮点数指数格式(大写). | |
f | 浮点数小数格式 | |
F | 浮点数小数格式 | |
g | 指数大于-4或者小于精度,则和e相同,反之则和 "f" 相同. | |
G | 指数大于-4或者小于精度,则和e相同,反之则和 "f" 相同. | |
c | 单字符 (接受整数或者单字符串). | |
r | 字符串 (将任意值转化为字符串,使用 repr()). | (3) |
s | 字符串 (将任意值转化为字符串,使用 str()). | (4) |
% | 不转换参数,结果为“ %”字符。 |
(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
%rconversion 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,
%sconversions do not assume that
'\0'is
the end of the string.
For safety reasons, floating point precisions are clipped to 50;
%fconversions for numbers whose absolute value is over 1e25 are replaced
by
%gconversions.2.9 All
other errors raise exceptions.
相关文章推荐
- Python之Fabric模块
- selenium2.0关于python的常用函数(二)
- python 3.5构建WINDOWS推送服务
- 习题10 那是什么
- pythonanywhere网站上开发和测试bottle框架构建的WEB网站
- 【Python】导入库numpy、pillow、scipy等问题汇总
- Python 转 EXE(知道真相的我眼泪掉下来)
- python常用模块汇总:time,os
- KD-Tree Python实现
- 安装pygame for Python3.5
- 习题9 打印,打印,打印
- Python--文件操作之os模块
- items,iteritems
- XDT——基于Python的XSS自动化检测工具
- python3实现简单爬虫功能
- python实现简单的web服务器
- 习题8 打印,打印
- Tkinter汇总
- 分析python处理基本数据<一>
- python学习笔记:extend和apend;strip,split,filter,map