<p>使用<a href="http://docs.python.org/library/string.html#format-string-syntax" rel="nofollow">str.format</a></p>
<pre><code>import math
n = float(raw_input("Example Number? "))
squareRootOfN = math.sqrt(n)
print('''\
{dashes}
{d:<16}{r:<15}{p:<}
{dashes}'''.format(dashes = '-'*50, d = ' # of Decimals', r = 'New Root', p = 'Percent error'))
for a in range(0,10):
preRoot = float(int(squareRootOfN * 10**a))
newRoot = preRoot/10**a
percentError = (n - newRoot**2)/n
print(' {d:<14}{r:<15}{p:13.9%}'.format(d = a, r = newRoot, p = percentError))
</code></pre>
<p>收益率</p>
<pre><code>--------------------------------------------------
# of Decimals New Root Percent error
--------------------------------------------------
0 9.0 18.181818182%
1 9.9 1.000000000%
2 9.94 0.198383838%
3 9.949 0.017574747%
4 9.9498 0.001494909%
5 9.94987 0.000087862%
6 9.949874 0.000007459%
7 9.9498743 0.000001428%
8 9.94987437 0.000000021%
9 9.949874371 0.000000001%
</code></pre>
<p>一些技巧/细节:</p>
<ul>
<li>您可以在
多行字符串。</li>
<li>格式为<code>{p:13.9%}</code>的百分比符号允许您离开
<code>percentError</code>作为十进制数(不乘以100)并且
把<code>%</code>放在最后。</li>
</ul>