黑松山资源网 Design By www.paidiu.com
老办法
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])
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年10月09日
2024年10月09日
- s14全球总决赛瑞士轮什么时候开始 s14瑞士轮开始时间介绍
- 晨报|《使命召唤21》全球发布预告 《异形:隔离》续作官宣开发中
- 隐形女联动毁灭博士?杰西卡·阿尔巴与小唐尼晒合影
- 《博德之门3》7号补丁发布后:Mod下载量达到2200万次!
- 徐璨宾《井底的蚯蚓》[FLAC/分轨][218.37MB]
- 王冰冰《Vaga Luna 月球漫游》[320K/MP3][62.76MB]
- 证声音乐图书馆《蔚蓝海岸·爵士度假日》[320K/MP3][59.79MB]
- 群星.1993-飞图合唱歌曲精丫飞图】【WAV+CUE】
- 王霏霏.2024-TOOOOO.BUSY(EP)【SNMUSIC】【FLAC分轨】
- 张宇.2016-好男人的情歌NEWXRCD【金牌大风】【WAV+CUE】
- 《食戟之灵》小林龙胆兔女郎比基尼版模型即将发售
- 重大里程碑!九号电动第500万台智能两轮电动车正式下线
- TGA主持人发文祝贺《暗喻幻想》 国外网友:提名稳了?
- 【原神】V5.0攻略 | 旋曜玉帛收集攻略(序号81~序号90)
- 【原神】V5.0攻略 | 旋曜玉帛收集攻略(序号91~序号100)