mccurcio
mccurcio

Reputation: 1344

Formatting in Python 2.7

I have a column formatting issue:

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, '%'

It comes out like:

enter image description here

Not in the same column!?!

Upvotes: 4

Views: 19588

Answers (3)

unutbu
unutbu

Reputation: 879481

Using str.format,

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))

yields

--------------------------------------------------
 # 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%

A few tricks/niceties:

  • Instead of three print statements, you can use one print statement on a multiline string.
  • The percent symbol in the format {p:13.9%} lets you leave percentError as a decimal (without multiplication by 100) and it places the % at the end for you.

Upvotes: 3

Mike Axiak
Mike Axiak

Reputation: 12004

@Bjorn has the right answer here, using the String.format specification. Python's string formatter has really powerful methods for aligning things properly. Here's an example:

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

Note that instead of tabs I'm using spaces to space things out. This is because tabs are really not what you want to use here, because the rules for how tabs space things are inconsistent (and depend on what your terminal/viewer settings are).

This is what the answer looks like:

---------------------------------------------------------------------------
 # 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 %

Upvotes: 10

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76788

This is how tabs work. To get a correct formatting, you should use string.format. For your example, it could look like this:

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

Upvotes: 0

Related Questions