Python中字符串的格式化方法小结


Posted in Python onMay 03, 2016

老办法

Python2.6之前,格式字符串的使用方法相对更简单些,虽然其能够接收的参数数量有限制。这些方法在Python3.3中仍然有效,但已有含蓄的警告称将完全淘汰这些方法,目前还没有明确的时间进度表。

格式化浮点数:

pi = 3.14159
print(" pi = %1.2f ", % pi)

多个替换值:

s1 = "cats"
s2 = "dogs"
s3 = " %s and %s living together" % (s1, s2)

没有足够的参数:

使用老的格式化方法,我经常犯错"TypeError: not enough arguments for formating string",因为我数错了替换变量的数量,编写如下这样的代码很容易漏掉变量。

set = (%s, %s, %s, %s, %s, %s, %s, %s) " % (a,b,c,d,e,f,g,h,i)

对于新的Python格式字符串,可以使用编号的参数,这样你就不需要统计有多少个参数。

set = set = " ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}) ".format(a,b,c,d,e,f,g)

Python 2.x 基于字典字符串格式化

"%(n)d %(x)s" %{"n":1, "x":"spam"}
reply = """
Greetings...
Hello %(name)s!
Your age squared is %(age)s
"""
values = {'name':'Bob', 'age':40}
print rely % values

Python 3.x format方法格式化

template = '{0},{1} and {2}'
template.format('spam','ham','eggs')

template = '{motto}, {pork} and {food}'
template.format(motto='spam', pork='ham', food='eggs')

template = '{motto}, {0} and {food}'
template.format('ham', motto='spam', food='eggs')

'{motto}, {0} and {food}'.format(42, motto=3.14, food=[1,2,3])
Python 相关文章推荐
python迭代器的使用方法实例
Nov 21 Python
wxpython 最小化到托盘与欢迎图片的实现方法
Jun 09 Python
Python编程中的反模式实例分析
Dec 08 Python
Python使用multiprocessing创建进程的方法
Jun 04 Python
python初学之用户登录的实现过程(实例讲解)
Dec 23 Python
python多进程使用及线程池的使用方法代码详解
Oct 24 Python
Python脚本完成post接口测试的实例
Dec 17 Python
python mysql断开重连的实现方法
Jul 26 Python
Python人工智能之路 jieba gensim 最好别分家之最简单的相似度实现
Aug 13 Python
Python confluent kafka客户端配置kerberos认证流程详解
Oct 12 Python
python UIAutomator2使用超详细教程
Feb 19 Python
你需要掌握的20个Python常用技巧
Feb 28 Python
Python实现约瑟夫环问题的方法
May 03 #Python
Python实现堆排序的方法详解
May 03 #Python
python web框架学习笔记
May 03 #Python
Python批量修改文本文件内容的方法
Apr 29 #Python
Python+Opencv识别两张相似图片
Mar 23 #Python
Python实现包含min函数的栈
Apr 29 #Python
Python二叉搜索树与双向链表转换实现方法
Apr 29 #Python
You might like
PHP小白必须要知道的php基础知识(超实用)
2017/10/10 PHP
Yii2 中实现单点登录的方法
2018/03/09 PHP
javascript在一段文字中的光标处插入其他文字
2007/08/26 Javascript
找到了一篇jQuery与Prototype并存的冲突的解决方法
2007/08/29 Javascript
JavaScript高级程序设计(第3版)学习笔记4 js运算符和操作符
2012/10/11 Javascript
javascript的内存管理详解
2013/08/07 Javascript
JavaScript修改css样式style动态改变元素样式
2013/12/16 Javascript
php和js对数据库图片进行等比缩放示例
2014/04/28 Javascript
javascript框架设计读书笔记之种子模块
2014/12/02 Javascript
JavaScript移除数组内重复元素的方法
2015/03/18 Javascript
纯javascript判断查询日期是否为有效日期
2015/08/24 Javascript
jQuery实时显示鼠标指针位置和键盘ASCII码
2016/03/28 Javascript
jQuery中使用animate自定义动画的方法
2016/05/29 Javascript
基本DOM节点操作
2017/01/17 Javascript
jquery实现tab键进行选择后enter键触发click行为
2017/03/29 jQuery
微信小程序实现跑马灯效果
2020/10/21 Javascript
用Python的urllib库提交WEB表单
2009/02/24 Python
python实现二级登陆菜单及安装过程
2019/06/21 Python
python中tkinter的应用:修改字体的实例讲解
2019/07/17 Python
Django分页功能的实现代码详解
2019/07/29 Python
python gdal安装与简单使用
2019/08/01 Python
Jupyter notebook如何修改平台字体
2020/05/13 Python
使用tensorflow实现VGG网络,训练mnist数据集方式
2020/05/26 Python
美国木工工具和用品商店:Woodcraft
2019/10/30 全球购物
Nike俄罗斯官方网站:Nike RU
2021/03/05 全球购物
硕士研究生自我鉴定
2013/11/08 职场文书
优秀毕业生自我鉴定
2014/01/19 职场文书
21岁生日感言
2014/02/27 职场文书
软件专业毕业生个人自我鉴定
2014/04/17 职场文书
基本公共卫生服务健康教育工作方案
2014/05/22 职场文书
基层党组织建设整改方案
2014/09/16 职场文书
个人租房协议书范本
2014/09/30 职场文书
车间质检员岗位职责
2015/04/08 职场文书
家属联谊会致辞
2015/07/31 职场文书
机械原理课程设计心得体会
2016/01/15 职场文书
Python接口自动化之文件上传/下载接口详解
2022/04/05 Python