擅长:python、mysql、java
<p>使用<a href="https://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters">^{<cd1>} variable arguments call syntax</a>传入元组:</p>
<pre><code>s = "x{}y{}z{}"
tup = (1,2,3)
s.format(*tup)
</code></pre>
<p>在<code>tup</code>之前的<code>*</code>告诉Python将元组解压成单独的参数,就好像您调用了<code>s.format(tup[0], tup[1], tup[2])</code>。</p>
<p>或者可以索引第一个位置参数:</p>
<pre><code>s = "x{0[0]}y{0[1]}z{0[2]}"
tup = (1,2,3)
s.format(tup)
</code></pre>
<p>演示:</p>
<pre><code>>>> tup = (1,2,3)
>>> s = "x{}y{}z{}"
>>> s.format(*tup)
'x1y2z3'
>>> s = "x{0[0]}y{0[1]}z{0[2]}"
>>> s.format(tup)
'x1y2z3'
</code></pre>