擅长:python、mysql、java
<p>想让你更容易创作出更复杂的创意</p>
<pre><code>print("The name is %s!" % names[1])
</code></pre>
<p>而不是</p>
^{pr2}$
<p>但是,由于您刚刚开始使用Python,您应该立即开始学习<a href="http://docs.python.org/library/string.html#new-string-formatting" rel="nofollow">new string formatting syntax</a>:</p>
<pre><code>print("The name is {}!".format(names[1])
</code></pre>
<p>当然,这个例子不能展示字符串格式化方法的真正威力。你可以做更多的事情,例如(摘自上面链接的文档):</p>
<pre><code>>>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated
'abracadabra'
>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
>>> 'Coordinates: {latitude}, {longitude}'.format(**coord)
'Coordinates: 37.24N, -115.81W'
>>> coord = (3, 5)
>>> 'X: {0[0]}; Y: {0[1]}'.format(coord)
'X: 3; Y: 5'
>>> # format also supports binary numbers
>>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)
'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'
</code></pre>
<p>等等。。。在</p>