python中format用法
来源:互联网 发布:有哪些视频剪辑软件 编辑:程序博客网 时间:2024/05/17 02:53
format是python2.6新增的一个格式化字符串的方法,相对于老版的%格式方法,它有很多优点。
format_spec 的格式
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 Kevin
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 Tom
class
Names():
name1='Kevin'
name2='Tom'
print
'hello {names.name1} i
am {names.name2}'.format(names=Names) #
hello Kevin i am Tom
数字格式输出描述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')
now=datetime.now()
print
'{:%Y-%m-%d %X}'.format(now)
print
"{!s}".format('2') #
2
print
"{!r}".format('2')
# '2'
1.不需要理会数据类型的问题,在%方法中%s只能替代字符串类型
2.单个参数可以多次输出,参数顺序可以不相同
3.填充方式十分灵活,对齐方式十分强大
4.官方推荐用的方式,%方式将会在后面的版本被淘汰
format的一个例子
1
print 'hello {0}'.format('world')
会输出hello world
format的格式
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.通过位置来填充字符串
1
2
3
'hello {0} i am {1}'.format('Kevin','Tom') #
hello Kevin i am Tom
'hello {} i am {}'.format('Kevin','Tom') #
hello Kevin i am Tom
'hello {0} i am {1} . my
name is {0}'.format('Kevin','Tom')
# hello Kevin i am Tom .
my name is Kevin
foramt会把参数按位置顺序来填充到字符串中,第一个参数是0,然后1 ……
也可以不输入数字,这样也会按顺序来填充
同一个参数可以填充多次,这个是format比%先进的地方
2.通过key来填充
1
print 'hello {name1} i am {name2}'.format(name1='Kevin',name2='Tom') # hello Kevin i am Tom
3.通过下标填充
1
2
3
names=['Kevin','Tom']
'hello {names[0]} i am
{names[1]}'.format(names=names) #
hello Kevin i am Tom
'hello {0[0]} i am {0[1]}'.format(names) #
hello Kevin i am Tom
4.通过字典的key
1
2
names={'name':'Kevin','name2':'Tom'}
print 'hello {names[name]} i am {names[name2]}'.format(names=names) # hello Kevin i am Tom
注意访问字典的key,不用引号的
5.通过对象的属性
1
2
3
4
5
class
Names():
name1='Kevin'
name2='Tom'
'hello {names.name1} i
am {names.name2}'.format(names=Names) #
hello Kevin i am Tom
6.使用魔法参数
1
2
3
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分别是二进制、十进制、八进制、十六进制。
三 对齐与填充
四 其他
1.转义{和}符号
1
'{{ hello {0} }}'.format('Kevin')
跟%中%%转义%一样,formate中用两个大括号来转义
2.format作为函数
1
2
f = 'hello {0} i am {1}'.format
print f('Kevin','Tom')
3.格式化datetime
1
2
now=datetime.now()
'{:%Y-%m-%d %X}'.format(now)
4.{}内嵌{}
1
print 'hello {0:>{1}} '.format('Kevin',50)
5.叹号的用法
!后面可以加s r a 分别对应str() repr() ascii()
作用是在填充前先用对应的函数来处理参数
1
2
"{!s}".format('2') #
2
"{!r}".format('2')
# '2'
差别就是repr带有引号,str()是面向用户的,目的是可读性,repr()是面向python解析器的,返回值表示在python内部的含义
ascii()一直报错,可能这个是3.0才有的函数
阅读全文
0 0
- python中format用法
- python中format用法
- Python中format简单用法
- python中format函数的用法
- Python中format函数用法说明
- Python Format用法
- Python format用法
- python:string.format用法
- python format用法
- python format 用法详解
- VC++中Format用法
- VC中format用法
- VC++中Format用法
- VC中format用法
- C++中Format用法
- VC++中Format用法
- python中format()方法
- python中format使用
- 反射:reflection
- Redis主从复制和集群配置
- 实现类似iOS拍照的按钮滑动功能
- 简单的模拟京东商城购买过程-pymysql
- slf4j+log4j+logback总结
- python中format用法
- 深入dubbo(四)配置参考
- 数据结构(Data Structure)(第三集)(链表(Linked List))
- POJ 3274.Gold Balanced Lineup
- 无缝轮播图的实现对比---CSS3版, JS版
- Python 3.6 正则表达式二 包含筛选IP正则表达式
- opencv----边缘检测:canny算子、sobel算子、laplace算子、scharr滤波器
- 猪八戒平台有点黑了吧,单没有接到,差点不见了一笔账
- 虚拟机安装CentOS不能联网问题