1 2 3 4 5 6 7 8 9 10 11 12 13 14
| print '{0}, {1}, {2}'.format('a', 'b', 'c') # a, b, c print '{}, {}, {}'.format('a', 'b', 'c') # a, b, c print '{2}, {1}, {0}'.format('a', 'b', 'c') # c, b, a print '{2}, {1}, {0}'.format(*'abc') # 參數(shù)解包 print '{0}{1}{0}'.format('hello-', 'world-') # 參數(shù)可以重復(fù)使用 # out: hello-world-hello-
# 命名參數(shù)例子 # out: 經(jīng)緯度: (37.24N, -115.81W) print '經(jīng)緯度: ({0}, {1})'.format('37.24N', '-115.81W') print 'A 經(jīng)緯度: ({latitude}, {longitude})'.format(latitude='37.24N', longitude='-115.81W') args = ('37.24N', '-115.81W') print 'B 經(jīng)緯度: ({}, {})'.format(*args) kwargs = {'latitude': '37.24N', 'longitude': '-115.81W'} print 'C 經(jīng)緯度: ({latitude}, {longitude})'.format(**kwargs)
|