<h3>关于多参数的元组/映射对象<code>format</code></h3>
<p>以下是文档的摘录:</p>
<blockquote>
<p>Given <code>format % values</code>, <code>%</code> conversion specifications in <code>format</code> are replaced with zero or more elements of <code>values</code>. The effect is similar to the using <code>sprintf()</code> in the C language.</p>
<p>If <code>format</code> requires a single argument, values may be a single non-tuple object. <em>Otherwise, values must be a tuple with exactly the number of items specified by the <code>format</code> string</em>, <em>or a single mapping object</em> (for example, a dictionary).</p>
</blockquote>
<h3>参考文献</h3>
<ul>
<li><a href="http://docs.python.org/library/stdtypes.html#string-formatting" rel="noreferrer">docs.python.org/library/stdtypes - string formatting</a></li>
</ul>
<hr/>
<h3>在<code>str.format</code>而不是<code>%</code></h3>上
<p>替代<code>%</code>运算符的新方法是使用<code>str.format</code>。以下是文档摘要:</p>
<blockquote>
<p><code>str.format(*args, **kwargs)</code></p>
<p>Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces <code>{}</code>. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument.</p>
<p><em>This method is the new standard in Python 3.0, and should be preferred to <code>%</code> formatting</em>.</p>
</blockquote>
<h3>参考文献</h3>
<ul>
<li><a href="http://docs.python.org/library/stdtypes.html#str.format" rel="noreferrer">docs.python.org/library/stdtypes - ^{<cd2>}</a>-<a href="http://docs.python.org/library/string.html#formatstrings" rel="noreferrer">syntax</a></li>
</ul>
<hr/>
<h3>示例</h3>
<p>下面是一些用法示例:</p>
<pre><code>>>> '%s for %s' % ("tit", "tat")
tit for tat
>>> '{} and {}'.format("chicken", "waffles")
chicken and waffles
>>> '%(last)s, %(first)s %(last)s' % {'first': "James", 'last': "Bond"}
Bond, James Bond
>>> '{last}, {first} {last}'.format(first="James", last="Bond")
Bond, James Bond
</code></pre>
<h3>另见</h3>
<ul>
<li><a href="http://docs.python.org/library/string.html#formatexamples" rel="noreferrer">docs.python.org/library/string - format examples</a></li>
</ul>