Python 字符串格式化 format
2017-07-29 00:00
489 查看
写在开头
比%更优雅的字符串格式化方式. def format(self, *args, **kwargs): # S.format(*args, **kwargs) -> string # Return a formatted version of S, using substitutions from args and kwargs. # The substitutions are identified by braces ('{' and '}').
格式化方式
通过索引格式化注意: "通过索引从format参数中的args中取值, 索引从0开始依次填充{0}, {1} ..." >>> print "{0} 的体重为 {1}KG".format("tianshl", 57) tianshl 的体重为 57KG >>> print "{1} 的体重为 {0}KG".format(57, "tianshl") tianshl 的体重为 57KG
注意: "format中参数个数不能少于实际要填充的{}的个数, 多余的参数会忽略" >>> print "{0} 的体重为 {1}KG".format("tianshl",) Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: tuple index out of range >>> print "{0} 的体重为 {1}KG, Ta的名字为 {0}".format("tianshl", 57) tianshl 的体重为 57KG, Ta的名字为 tianshl >>> print "{0} 的体重为 {1}KG".format("tianshl", 57, "多余的参数") tianshl 的体重为 57KG
注意: "若{}不写索引则默认从0开始依次加一" >>> print "{} 的体重为 {}KG".format("tianshl", 57) tianshl 的体重为 57KG
通过关键字格式化
注意: "通过key取format中的参数kwargs的值" >>> print "{name} 的体重为 {weight}KG".format(name="tianshl", weight=57) tianshl 的体重为 57KG
通过列表或元组格式化
>>> tianshl = ['tianshl', 57] # tianshl = ('tianshl', 57) >>> print "{0} 的体重为 {1}KG".format(*tianshl) tianshl 的体重为 57KG
通过字典格式化
>>> tianshl = {'name': 'tianshl', 'weight': 57} >>> print "{name} 的体重为 {weight}KG".format(**tianshl) tianshl 的体重为 57KG
通过对象的属性格式化
>>> class People: ... def __init__(self, name, weight): ... self.name = name ... self.weight = weight ... >>> tianshl = People('tianshl', 57) >>> print "{people.name} 的体重为 {people.weight}KG".format(people=tianshl) tianshl 的体重为 57KG
以上格式化方式可以混合使用
>>> print "{} 的体重为 {weight}KG".format("tianshl", weight=57) tianshl 的体重为 57KG
格式限定符
填充与对齐格式: {:[填充符][对齐方式][字符长度]} 解释: 1. 填充符: 任意一个字符, 默认为一个空格 2. 对齐方式: 左对齐(<), 居中(^), 右对齐(>), 默认为右对齐 3. 字符长度: 字符串的长度, 默认为插值的长度 举例: 为了明显字符串两边以|作为边界 # 三个选项都为默认值 >>> print '|{:}|'.format(1) |1| # 前两项为默认值, 长度设置为8 >>> print '|{:8}|'.format(1) | 1| # 填充符默认, 左对齐, 长度为8 >>> print '|{:<8}|'.format(1) |1 | # 填充符为$, 居中, 长度为3 >>> print '|{:$^3}|'.format("_") |$_$|
浮点类型精度
格式: {:.1f} 其中1为小数位数, 设置为0时保留整数 特点: 四舍五入 >>> print '{:.1f}'.format(0.44444) 0.4 >>> print '{:.1f}'.format(0.55555) 0.6 >>> print '{:.0f}'.format(0.55555) 1 # 使用内置的format()方法效果一样 >>> format(0.55555, '.1f') '0.6'
逗号作为千位分隔符
>>> print '{:,}'.format(55555555555) 55,555,555,555
进制转换
格式{:[进制]} 进制可选项: [b, o, d, x] 分别为二进制, 八进制, 十进制, 十六进制 注意: format的参数为十进制 >>> print "{:b}".format(15) 1111 >>> print "{:o}".format(15) 17 >>> print "{:d}".format(15) 15 >>> print "{:x}".format(15) f
其他类型
'b' : 二进制。将数字以2为基数进行输出。 'c' : 字符。在打印之前将整数转换成对应的Unicode字符串。 'd' : 十进制整数。将数字以10为基数进行输出。 'o' : 八进制。将数字以8为基数进行输出。 'x' : 十六进制。将数字以16为基数进行输出,9以上的位数用小写字母。 'e' : 幂符号。用科学计数法打印数字。用'e'表示幂。 'g' : 一般格式。将数值以fixed-point格式输出。当数值特别大的时候,用幂形式打印。 'n' : 数字。当值为整数时和'd'相同,值为浮点数时和'g'相同。不同的是它会根据区域设置插入数字分隔符。 '%' : 百分数。将数值乘以100然后以fixed-point('f')格式打印,值后面会有一个百分号
写在最后
原样输出{} 需要对{} 转义方式: 双写 {{}} 测试: >>> print "这个需要格式化{}, 这个不需要格式化{{}}".format('$') 这个需要格式化$, 这个不需要格式化{}
相关文章推荐
- python: str.format (字符串 格式化)
- python format格式化字符串
- 浅谈Python 字符串格式化输出(format/printf)
- python中format()方法格式化字符串
- Python:字符串格式化输出(format/printf)
- python,格式化字符串函数.format()的用法
- python 字符串格式化(format)
- Python 字符串格式化输出(format/printf)
- python 中函数format()函数进行字符串格式化
- Python中字符串格式化:%和format
- Python——format字符串格式化
- Python小白学习之路(九)—【字符串格式化】【百分号方式】【format方式】
- Python .format(),%格式化时,对字符串编码方式的影响
- Python中print/format字符串格式化实例
- python之字符串格式化(format)
- Python之字符串格式化,"format"替代"%"
- python 之 格式化字符串函数format()
- Python用format格式化字符串
- python格式化字符串Type Error: Format Requires Mapping 的问题
- Python - 格式化(format())输出字符串 详解 及 代码