- 当一个字符串包含多个相同的变量时,使用如下方法:
代码:
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
网友评论