1 >>> string = 'python{}, django{}, tornado{}'.format(2.7, 'web', 'tornado') # 有多少个{}占位符就有多少个值与其对应,按照顺序“填”进字符串中 2 >>> string 3 'python2.7, djangoweb, tornadotornado' 4 >>> string = 'python{}, django{}, tornado{}'.format(2.7, 'web') 5 Traceback (most recent call last): 6 File "<pyshell#6>", line 1, in <module> 7 string = 'python{}, django{}, tornado{}'.format(2.7, 'web') 8 IndexError: tuple index out of range 9 >>> string = 'python{0}, django{2}, tornado{1}'.format(2.7, 'web', 'tornado') # 也可以指定“填”进去的值(从0开始,后面的值不一定都要用上,但是要保证指定的位置是有值的)10 >>> string11 'python2.7, djangotornado, tornadoweb'12 >>> string = 'python{py}, django{dja}, tornado{tor}'.format(tor='tornado', dja='web', py=2.7) # 可以使用键值对的形式赋值13 >>> string14 'python2.7, djangoweb, tornadotornado'15 >>>
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did84960