Python技巧

2019-01-18  本文已影响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
上一篇下一篇

猜你喜欢

热点阅读