python字符串格式化输出1
来源:互联网 发布:视频dvd刻录软件 编辑:程序博客网 时间:2024/06/05 05:55
str.format()
位置传递参数
>>> "{0} {0} {1}".format("big","world")
'big big world'
值传递参数
>>>print ("name :{name},age :{age}".format(name="bob",age=25))
name :bob,age :25
字典传递参数
>>> info = {"name":"bob","age":25,"weight":80}
>>> print ("name:{name},age:{age}".format(**info))
name:bob,age:25
通过索引传递参数
>>> list = ['bob',25,"good"]
>>> print ("name:{0[0]}".format(list))
数字格式化
保留n位小数
>>> print ("{:.3f}".format(3.1415926))
3.142
不保留小数
>>> print ("{:.0f}".format(3.1415926))
3
左边补0
>>> print ("{:0>3d}".format(3))
003
右边补字母
>>> print ("{:x<3d}".format(3))
3xx
参数传递注意:
>>> a
('bob', 'name')
>>> b
['bob', 'name']
>>> print ("name1:{0[0]},name2:{1[0]}".format(a,b))
name1:bob,name2:bob
>>> print ("name1:{0[0]},name2:{1[1]}".format(a,b))
name1:bob,name2:name
此外我们可以使用大括号 {} 来转义大括号,如下实例:
>>> print ("name :{0[1]} 的位置是 {{0[1]}}".format(a))
name :name 的位置是 {0[1]}
>>> a
('bob', 'name')
- python字符串格式化输出1
- python 字符串格式化 输出
- python字符串格式化输出
- Python字符串格式化输出
- Python 字符串格式化输出(format/printf)
- Python:字符串格式化输出(format/printf)
- Python第一课,字符串的格式化输出
- Python里字符串转数字格式化输出
- [Python] 格式化输出(1)
- Python - 格式化(format())输出字符串 详解 及 代码
- python中格式化字符串输出的方法总结
- python-格式化(format())输出字符串 详解 及 代码
- Python - 格式化(format())输出字符串 详解 及 代码
- python日期时间转为字符串或者格式化输出
- 格式化输出字符串
- printf格式化输出字符串
- 字符串格式化输出
- Android字符串格式化输出
- 【Python学习系列八】Python实现线性可分SVM(支持向量机)
- mybatis <foreach>标签
- 最长上升子序列
- NP完全问题
- WebappClassLoaderBase.clearReferencesJdbc
- python字符串格式化输出1
- 使用 XPath 解析 HTML 文档
- 查词典(查一个词后面的东西)
- hihoCoder 1105 : 题外话·堆
- flume-NG整合hdfs和kafka
- 简单常用的一个reset.css
- QT QML初体验随笔之QQuickView(11)
- android中shape的使用
- 简述 Docker比你想象的简单