美文网首页
Python技巧

Python技巧

作者: 峰峰仔 | 来源:发表于2019-01-18 14:58 被阅读0次
  1. 当一个字符串包含多个相同的变量时,使用如下方法:
    代码:
str = '%(foo)s is %(foo)s is %(foo)s' %{'foo': 'look at'}
print str
str1 = '{0} {0} {1}'.format('look at', 'python')
print str1

str2 = '{foo} {foo} is {snow}'.format(foo='look at', snow='world')
print str2

输出:

look at is look at is look at
look at look at python
look at look at is world

相关文章

网友评论

      本文标题:Python技巧

      本文链接:https://www.haomeiwen.com/subject/oqendqtx.html