Python之str操作方法(详解)
发布时间:2020-12-17 08:14:45 所属栏目:Python 来源:网络整理
导读:1. str.format():使用“{}”占位符格式化字符串(占位符中的索引号形式和键值对形式可以混合使用)。 string = 'python{},django{},tornado{}'.format(2.7,'web','tornado') # 有多少个{}占位符就有多少个值与其对应,按照顺序“填”进字符串中 string'pyth
1. str.format():使用“{}”占位符格式化字符串(占位符中的索引号形式和键值对形式可以混合使用)。 >>> string = 'python{},django{},tornado{}'.format(2.7,'web','tornado') # 有多少个{}占位符就有多少个值与其对应,按照顺序“填”进字符串中 >>> string 'python2.7,djangoweb,tornadotornado' >>> string = 'python{},'web') Traceback (most recent call last): File "<pyshell#6>",line 1,in <module> string = 'python{},'web') IndexError: tuple index out of range >>> string = 'python{0},django{2},tornado{1}'.format(2.7,'tornado') # 也可以指定“填”进去的值(从0开始,后面的值不一定都要用上,但是要保证指定的位置是有值的) >>> string 'python2.7,djangotornado,tornadoweb' >>> string = 'python{py},django{dja},tornado{tor}'.format(tor='tornado',dja='web',py=2.7) # 可以使用键值对的形式赋值 >>> string 'python2.7,tornadotornado' >>> 2. 使用“%”进行字符串格式化。 格式化符号表
辅助格式化符号表
以上这篇Python之str操作方法(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |