Python用format格式化字符串
2014-11-12 18:05
393 查看
format是是python2.6新增的一个格式化字符串的方法,相对于老版的%格式方法,它有很多优点。1.不需要理会数据类型的问题,在%方法中%s只能替代字符串类型2.单个参数可以多次输出,参数顺序可以不相同3.填充方式十分灵活,对齐方式十分强大4.官方推荐用的方式,%方式将会在后面的版本被淘汰format的一个例子
field_name ::= arg_name ("." attribute_name | "[" element_index "]")*
arg_name ::= [identifier | integer]
attribute_name ::= identifier
element_index ::= integer | index_string
index_string ::= <any source character except "]"> +
conversion ::= "r" | "s" | "a"
format_spec ::= <described in the next section>
format_spec 的格式format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
fill ::= <any character>
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::= integer
precision ::= integer
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"应用:一 填充1.通过位置来填充字符串
三 对齐与填充
四 其他1.转义{和}符号
print 'hello {0}'.format('world')会输出hello worldformat的格式replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name ::= arg_name ("." attribute_name | "[" element_index "]")*
arg_name ::= [identifier | integer]
attribute_name ::= identifier
element_index ::= integer | index_string
index_string ::= <any source character except "]"> +
conversion ::= "r" | "s" | "a"
format_spec ::= <described in the next section>
format_spec 的格式format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
fill ::= <any character>
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::= integer
precision ::= integer
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"应用:一 填充1.通过位置来填充字符串
print 'hello {0} i am {1}'.format('Kevin','Tom') # hello Kevin i am Tom print 'hello {} i am {}'.format('Kevin','Tom') # hello Kevin i am Tom print 'hello {0} i am {1} . my name is {0}'.format('Kevin','Tom') # hello Kevin i am Tom . my name is Kevinforamt会把参数按位置顺序来填充到字符串中,第一个参数是0,然后1 ……也可以不输入数字,这样也会按顺序来填充同一个参数可以填充多次,这个是format比%先进的地方2.通过key来填充
print 'hello {name1} i am {name2}'.format(name1='Kevin',name2='Tom') # hello Kevin i am Tom3.通过下标填充
names=['Kevin','Tom'] print 'hello {names[0]} i am {names[1]}'.format(names=names) # hello Kevin i am Tom print 'hello {0[0]} i am {0[1]}'.format(names) # hello Kevin i am Tom4.通过字典的key
names={'name':'Kevin','name2':'Tom'} print 'hello {names[name]} i am {names[name2]}'.format(names=names) # hello Kevin i am Tom注意访问字典的key,不用引号的5.通过对象的属性
class Names():6.使用魔法参数
name1='Kevin'
name2='Tom'print 'hello {names.name1} i am {names.name2}'.format(names=Names) # hello Kevin i am Tom
args=['lu'] kwargs = {'name1': 'Kevin', 'name2': 'Tom'} print 'hello {name1} {} i am {name2}'.format(*args, **kwargs) # hello Kevin i am Tom二 格式转换b、d、o、x分别是二进制、十进制、八进制、十六进制。
数字 | 格式 | 输出 | 描述 |
3.1415926 | {:.2f} | 3.14 | 保留小数点后两位 |
3.1415926 | {:+.2f} | 3.14 | 带符号保留小数点后两位 |
-1 | {:+.2f} | -1 | 带符号保留小数点后两位 |
2.71828 | {:.0f} | 3 | 不带小数 |
1000000 | {:,} | 1,000,000 | 以逗号分隔的数字格式 |
0.25 | {:.2%} | 25.00% | 百分比格式 |
1000000000 | {:.2e} | 1.00E+09 | 指数记法 |
25 | {0:b} | 11001 | 转换成二进制 |
25 | {0:d} | 25 | 转换成十进制 |
25 | {0:o} | 31 | 转换成八进制 |
25 | {0:x} | 19 | 转换成十六进制 |
数字 | 格式 | 输出 | 描述 |
5 | {:0>2} | 05 | 数字补零 (填充左边, 宽度为2) |
5 | {:x<4} | 5xxx | 数字补x (填充右边, 宽度为4) |
10 | {:x^4} | x10x | 数字补x (填充右边, 宽度为4) |
13 | {:10} | 13 | 右对齐 (默认, 宽度为10) |
13 | {:<10} | 13 | 左对齐 (宽度为10) |
13 | {:^10} | 13 | 中间对齐 (宽度为10) |
print '{{ hello {0} }}'.format('Kevin')跟%中%%转义%一样,formate中用两个大括号来转义2.format作为函数
f = 'hello {0} i am {1}'.format print f('Kevin','Tom')3.格式化datetime
now=datetime.now() print '{:%Y-%m-%d %X}'.format(now)4.{}内嵌{}
print 'hello {0:>{1}} '.format('Kevin',50)5.叹号的用法!后面可以加s r a 分别对应str() repr() ascii()作用是在填充前先用对应的函数来处理参数
print "{!s}".format('2') # 2 print "{!r}".format('2') # '2'差别就是repr带有引号,str()是面向用户的,目的是可读性,repr()是面向python解析器的,返回值表示在python内部的含义ascii()一直报错,可能这个是3.0才有的函数参考:https://docs.python.org/3/library/string.html#grammar-token-conversion
相关文章推荐
- Python - 格式化(format())输出字符串 详解 及 代码
- Python中print/format字符串格式化实例
- Python中字符串格式化str.format的详细介绍
- 神一般的 str.format() 函数 - Python 字符串格式化转换(转载)
- Python .format(),%格式化时,对字符串编码方式的影响
- Python - 格式化(format())输出字符串 详解 及 代码
- Python--format 函数格式化字符串的用法
- Python基础-字符串格式化_百分号方式_format方式
- Python基础-字符串格式化_百分号方式_format方式(转)
- python 给urllib.quote的字符串格式化出现ValueError: unsupported format character
- Python中用format()格式化字符串
- python格式化字符串Type Error: Format Requires Mapping 的问题
- Python格式化字符串format
- Python 字符串格式化输出(format/printf)
- Python:字符串格式化输出(format/printf)
- 浅谈Python 字符串格式化输出(format/printf)
- python笔记---字符串格式化函数format
- Python基础-字符串格式化_百分号方式_format方式
- Python 字符串格式化 format
- python 中函数format()函数进行字符串格式化