Python2.7中的格式设置

Python2.7中的格式设置,python,python-2.7,Python,Python 2.7,我有一个列格式问题: from math import sqrt n = raw_input("Example Number? ") n = float(n) sqaureRootOfN = sqrt(n) print '-'*50 print ' # of Decimals', '\t', 'New Root', '\t', 'Percent error' print '-'*50 for a in range(0,10): preRoot = float(int

我有一个列格式问题:

from math import sqrt
n = raw_input("Example Number? ")
n = float(n)
sqaureRootOfN = sqrt(n)

print '-'*50
print ' # of Decimals', '\t', 'New Root', '\t', 'Percent error'
print '-'*50
for a in range(0,10):
    preRoot = float(int(sqaureRootOfN * 10**a))
    newRoot = preRoot/10**a
    percentError = (n - newRoot**2)/n*100
    print ' ', a, '\t\t', newRoot, '\t\t', percentError, '%'
结果是:


不在同一列中

选项卡就是这样工作的。要获得正确的格式,应使用。例如,它可能如下所示:

print "{0:2d}          {1:9.8f} {2:f} %".format(a, newRoot, percentError)

@Bjorn使用String.format规范在这里给出了正确的答案。Python的字符串格式化程序有非常强大的方法来正确地对齐内容。下面是一个例子:

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) + ' %')
使用

一些技巧/细节:

  • 您可以在上使用一条打印语句,而不是三条打印语句 多行字符串
  • 格式为
    {p:13.9%}
    的百分比符号允许您离开
    percentError
    作为十进制数(不乘以100),并且 将
    %
    放在末尾

这是因为您将两个选项卡放在一起,而不是一个选项卡。我自己,我想我应该把每一行构建成一个字符串,在添加每个数字之间添加适当数量的空格。使用python字符串格式的方法,
format
%
,它们不会给出所需的输出。新根列中的值都有相同的小数位数输出。
“{0:2d}{1:20.8f}{2:f}%”。format
打印相同的值,并且您不会将文字间距与格式间距“{0:2d}{1:20.8f}{2:f}%”混合使用。format:似乎对2.7不起作用,这是因为您使用的是python 2.6。这个问题专门讨论2.7。别再无缘无故地投反对票了。这对我来说很简单,我喜欢。
---------------------------------------------------------------------------
 # 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 %
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))
--------------------------------------------------
 # 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%