SAPJV
SAPJV

Reputation: 115

How to display values in simple tabular format without using format()

I am trying to make a simple program in python and I want to display my output in a simple tabular format. But the alignment is getting disturbed every time. The constraint is not to use format() of Python.

I am trying to print required output using string format in python. But I am unable to get the required output. Please help me.

I have tried this :

def footToMeter(foot):
    Meter = 0.305 * foot
    return Meter


def meterToFoot(meter):
    Foot = meter / 0.305
    return Foot

i = 1.0
j = 20.0

header = ('Feet','Meters    |    ','Meters','Feet')

print("%-14s%-15s%-15s%-15s" % header)

while i<=15:
    print("%s" % i ,"%15.3f" % footToMeter(i),"    |   ",j,"%16.3f" % 
meterToFoot(j))
    i = i + 1
    j = j + 6

Actual output should be in perfectly left aligned tabular format ( check below output ) . But alignment of my output is slightly disturbed.

enter image description here

Upvotes: 1

Views: 298

Answers (3)

tevemadar
tevemadar

Reputation: 13225

Minimal changes to your code would be

old: print( "%s" % i ,"%15.3f" % footToMeter(i),"    |   ",    j,"%16.3f" % meterToFoot(j))
new: print("%4d" % i ,"%15.3f" % footToMeter(i),"   |","%9d" % j,"%12.3f" % meterToFoot(j))

So the key is to have a fixed width for everything (i and j did not have one in the original), and then it was just a play with the numbers (%15.3f has not even changed).

Result looks like this:

Feet          Meters    |    Meters         Feet           
   1           0.305    |        20       65.574
   2           0.610    |        26       85.246
   3           0.915    |        32      104.918
   4           1.220    |        38      124.590
   5           1.525    |        44      144.262
   6           1.830    |        50      163.934
   7           2.135    |        56      183.607
   8           2.440    |        62      203.279
   9           2.745    |        68      222.951
  10           3.050    |        74      242.623
  11           3.355    |        80      262.295
  12           3.660    |        86      281.967
  13           3.965    |        92      301.639
  14           4.270    |        98      321.311
  15           4.575    |       104      340.984

You can see it in action here: https://ideone.com/9UKuw1


The same approach can be used for left-aligning too:

old: print(   "%s" % i ,"%15.3f" % footToMeter(i),"    |   ",      j, "%16.3f" % meterToFoot(j))
new: print("%-13d" % i ,"%-9.3f" % footToMeter(i),"|   ","%-14d" % j,"%-12.3f" % meterToFoot(j))

Though I consider the result (of left-alignment) ugly:

Feet          Meters    |    Meters         Feet           
1             0.305     |    20             65.574      
2             0.610     |    26             85.246      
3             0.915     |    32             104.918     
4             1.220     |    38             124.590     
5             1.525     |    44             144.262     
6             1.830     |    50             163.934     
7             2.135     |    56             183.607     
8             2.440     |    62             203.279     
9             2.745     |    68             222.951     
10            3.050     |    74             242.623     
11            3.355     |    80             262.295     
12            3.660     |    86             281.967     
13            3.965     |    92             301.639     
14            4.270     |    98             321.311     
15            4.575     |    104            340.984

Or see it in action at https://ideone.com/RatzqW

Upvotes: 0

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20500

You can use tabs \t and play around with the spaces and formatting strings to get your display right, one example is

def footToMeter(foot): 
    return  0.305 * foot 

def meterToFoot(meter): 
    return meter / 0.305 

i = 1.0
j = 20.0

#Replaced spaces with a mix of tabs and spaces
print('Feet\t\t Meters\t\t|\t\t Meters\t\tFeet')
print()

while i<=15:
    #Played around with format strings
    print("%-4.1f\t\t" % i ,"%-.3f" % footToMeter(i),"\t\t|\t\t",j,"\t\t%-7.3f" %meterToFoot(j))
    i = i + 1
    j = j + 6

This gives me the table

Feet         Meters     |        Meters     Feet

1.0          0.305      |        20.0       65.574 
2.0          0.610      |        26.0       85.246 
3.0          0.915      |        32.0       104.918
4.0          1.220      |        38.0       124.590
5.0          1.525      |        44.0       144.262
6.0          1.830      |        50.0       163.934
7.0          2.135      |        56.0       183.607
8.0          2.440      |        62.0       203.279
9.0          2.745      |        68.0       222.951
10.0         3.050      |        74.0       242.623
11.0         3.355      |        80.0       262.295
12.0         3.660      |        86.0       281.967
13.0         3.965      |        92.0       301.639
14.0         4.270      |        98.0       321.311
15.0         4.575      |        104.0      340.984

Upvotes: 2

J...S
J...S

Reputation: 5207

You could use f-strings. They are available from Python3.6 onwards.

As in

i = 1.0
j = 20.0

header = ('Feet','Meters')
print (f"{header[0]:<10}{header[1]:<10s}    |    {header[1]:<10s}{header[0]:<10s}\n")

while i<=15:
    print(f"{i:<10.1f}{footToMeter(i):<10.3f}    |    {j:<10.1f}{meterToFoot(j):<10.3f}")
    i = i + 1
    j = j + 6

To get output like

Feet      Meters        |    Meters    Feet      

1.0       0.305         |    20.0      65.574    
2.0       0.610         |    26.0      85.246    
3.0       0.915         |    32.0      104.918   
4.0       1.220         |    38.0      124.590   
5.0       1.525         |    44.0      144.262   
6.0       1.830         |    50.0      163.934   
7.0       2.135         |    56.0      183.607   
8.0       2.440         |    62.0      203.279   
9.0       2.745         |    68.0      222.951   
10.0      3.050         |    74.0      242.623   
11.0      3.355         |    80.0      262.295   
12.0      3.660         |    86.0      281.967   
13.0      3.965         |    92.0      301.639   
14.0      4.270         |    98.0      321.311   
15.0      4.575         |    104.0     340.984

The < is used for left alignment.

Modification to the header tuple and field widths were made.

You may be interested in the PEP as well.

Upvotes: 1

Related Questions