Blog icon indicating copy to clipboard operation
Blog copied to clipboard

format 用法

Open codcodog opened this issue 7 years ago • 0 comments

format 用法

介绍 Python3 的 str.format() 函数用法

如果只是简单的字符映射,可以使用 Python2 的 % 格式化

映射参数

占位符映射

>>> sammy_string = "Sammy loves {} {}, and has {} {}." 
>>> sammy_string.format("open-source", "software", 5, "balloons")
'Sammy loves open-source software, and has 5 balloons.'

位置映射

>>> "Sammy the {0} has a pet {1}!".format("shark", "pilot fish")
'Sammy the shark has a pet pilot fish!'

>>> "Sammy is a {3}, {2}, and {1} {0}!".format("happy", "smiling", "blue", "shark")
'Sammy is a shark, blue, and smiling happy!'

关键字映射

>>> '{name}, {age}'.format(age=18, name='kzc')
'kzc, 18'

对象属性

>>> class Person:
...        def __init__(self, name, age):
...            self.name, self.age = name, age
...        def __str__(self):
...            return 'This guy is {self.name}, is {self.age} old.'.formate(self=self)

>>> str(Person('kzc', 18))
'This guy is kzc, is 18 old.'

通过下标

>>> p=['kzc', 18]
>>> '{0[0]}, {0[1]}'.format(p)
'kzc, 18'

格式限定符

^, <, > 分别是居中,左对齐,右对齐
: 后面带填充的字符,只能是一个字符,不指定默认填充空格

>>> '{:>8}'.format('189')
'     189'

>>> '{:0>8}'.format('189')
'00000189'

>>> '{:.2f}'.format(321.33345)
'321.33'

f 表示 float 类型

格式限定符的一般形式:

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

fill 可选的填充字符,用来填充到最小宽度的字符,如果填充字符存在必须跟随一个对齐标志

align 对齐标志:^, <, >, =

sign 只对数字类型有效, 符号可以是:+, -, 空格

# 存在的话,则二进制,八进制,十六进制输出将以 0b, 0o, 0x 为前缀

>>> '{:#x}'.format(15)
'0xf'
>>> '{:x}'.format(15)
'f'

width 如果前面有 0 则表示使用 0 填充, 相当于 =0

>>> '{:010}'.format(239)
'0000000239'
>>> '{:10}'.format(239)
'       239'

minimumwidth 宽度

precision 精度

type 数据类型

详细参考:PEP 3101 -- Advanced String Formatting

参考文章:
How To Use String Formatters in Python 3
飘逸的python - 增强的格式化字符串format函数

codcodog avatar Jan 07 '18 09:01 codcodog