python format 用法详解
来源:互联网 发布:nginx配置stream 编辑:程序博客网 时间:2024/05/17 03:24
前序:format是python2.6新增的一个格式化字符串的方法,相对于老版的%格式方法,它有很多优点。
- 不需要理会数据类型的问题,在%方法中%s只能替代字符串类型
- 单个参数可以多次输出,参数顺序可以不相同
- 填充方式十分灵活,对齐方式十分强大
- 官方推荐用的方式,%方式将会在后面的版本被淘汰
format填充字符串
一 填充
1.通过位置来填充字符串
print('hello {0} i am {1}'.format('world','python')) # 输入结果:hello world i am pythonprint('hello {} i am {}'.format('world','python') ) #输入结果:hello world i am pythonprint('hello {0} i am {1} . a now language-- {1}'.format('world','python')# 输出结果:hello world i am python . a now language-- python
foramt会把参数按位置顺序来填充到字符串中,第一个参数是0,然后1 ……
也可以不输入数字,这样也会按顺序来填充
同一个参数可以填充多次,这个是format比%先进的地方
2.通过key来填充
obj = 'world'name = 'python'print('hello, {obj} ,i am {name}'.format(obj = obj,name = name))# 输入结果:hello, world ,i am python
3.通过列表填充
list=['world','python']print('hello {names[0]} i am {names[1]}'.format(names=list))# 输出结果:hello world i am pythonprint('hello {0[0]} i am {0[1]}'.format(list)) #输出结果:hello world i am python
4.通过字典填充
dict={‘obj’:’world’,’name’:’python’}
print(‘hello {names[obj]} i am {names[name]}’.format(names=dict)) # hello world i am python
注意访问字典的key,不用引号的
5.通过类的属性填充
class Names(): obj='world' name='python'print('hello {names.obj} i am {names.name}'.format(names=Names))#输入结果hello world i am python
6.使用魔法参数
args = [‘,’,’inx’]
kwargs = {‘obj’: ‘world’, ‘name’: ‘python’}
print(‘hello {obj} {} i am {name}’.format(*args, **kwargs))#输入结果:hello world , i am python
注意:魔法参数跟你函数中使用的性质是一样的:这里format(*args, **kwargs)) 等价于:format(‘,’,’inx’,obj = ‘world’,name = ‘python’)
format 格式转换
b、d、o、x分别是二进制、十进制、八进制、十六进制。
其它用法
1.转义“{}”
print('{{hello}} {{{0}}}'.format('world')) #输出结果:{hello} {world}
跟%中%%转义%一样,format中用 { 来转义{ ,用 } 来转义 }
2.format作为函数变量
name = 'InX'hello = 'hello,{} welcome to python world!!!'.format #定义一个问候函数hello(name) #输入结果:hello,inx welcome to python world!!!
3.格式化datetime
from datetime import datetimenow=datetime.now()print '{:%Y-%m-%d %X}'.format(now) # 输出结果:2017-07-24 16:51:42
4.{}内嵌{}
print('hello {0:>{1}} '.format('world',10)) ##输出结果:hello world
从最内层的{}开始格式化
5.叹号的用法
print(‘{!s}国’.format(‘中’)) #输出结果:中国
print(‘{!a}国’.format(‘中’)) #输出结果:’\u4e2d’国
print(‘{!s}国’.format(‘中’)) #输出结果:’中’国
!后面可以加s r a 分别对应str() repr() ascii() 作用是在填充前先用对应的函数来处理参数
差别就是repr带有引号,str()是面向用户的,目的是可读性,repr()是面向Python解析器的,返回值表示在python内部的含义,ascii (),返回ascii编码
更多参考:https://docs.python.org/3/library/string.html#grammar-token-conversion
- python format 用法详解
- Python Format用法
- Python format用法
- python:string.format用法
- python中format用法
- python format用法
- python中format用法
- Format用法详解
- Format用法详解
- Python format函数详解
- Python的format方法用法
- python 学习 笔记 format 用法
- Python中format简单用法
- Python string.format() 的用法
- access中的Format用法详解
- python string.format的用法总结
- python str.format()方法的基本用法
- python中format函数的用法
- Html5新增常见标签
- 将"eh"更正为"Excel Home"
- 51nod_1179_计算N个数之间两两之间GCD的最大值
- ADB个人用法
- android图片大小的要求质量压缩图片
- python format 用法详解
- 昂贵的聘礼 POJ
- file保存成blob
- string 整形数据 相互转化
- IntelliJ IDEA社区版Community创建maven并布置到tomcat
- HDU--dp练习--1009--命运
- 微服务的基础框架选择:SpringCloud还是Dubbo?
- Linux服务器提示No space left on device错误解决方法
- leetcode[Minimum Moves to Equal Array Elements]