<p>@Bjorn使用String.format规范给出了正确的答案。Python的字符串格式化程序有非常强大的方法来正确地对齐事物。下面是一个例子:</p>
<pre><code>from math import sqrt
n = raw_input("Example Number? ")
n = float(n)
sqaureRootOfN = sqrt(n)
print '-'*75
print ' # of Decimals', ' ' * 8, 'New Root', ' ' * 10, 'Percent error'
print '-'*75
for a in range(0,10):
preRoot = float(int(sqaureRootOfN * 10**a))
newRoot = preRoot/10**a
percentError = (n - newRoot**2)/n*100
print " {: <20}{: <25}{: <18}".format(a, newRoot, str(percentError) + ' %')
</code></pre>
<p>注意,我使用空格来分隔东西,而不是制表符。这是因为制表符<em>实际上</em>不是您想在这里使用的,因为制表符的空间规则不一致(并且取决于您的终端/查看器设置)。</p>
<p>答案是这样的:</p>
<pre><code>---------------------------------------------------------------------------
# of Decimals New Root Percent error
---------------------------------------------------------------------------
0 9.0 18.1818181818 %
1 9.9 1.0 %
2 9.94 0.198383838384 %
3 9.949 0.0175747474747 %
4 9.9498 0.00149490909092 %
5 9.94987 8.7861717162e-05 %
6 9.949874 7.45871112931e-06 %
7 9.9498743 1.4284843602e-06 %
8 9.94987437 2.14314187048e-08 %
9 9.949874371 1.33066711409e-09 %
</code></pre>