-1

Not really sure how to title this question but basically I'm wondering if there is a way to make this:

Feet   Meters   |      Meters   Feet
----   ------   |      ------   ----
1      0.305    |      1        3.281
2      0.61    |      2        6.562
3      0.914    |      3        9.843
4      1.219    |      4        13.123
5      1.524    |      5        16.404
6      1.829    |      6        19.685
7      2.134    |      7        22.966
8      2.438    |      8        26.247
9      2.743    |      9        29.528
10      3.048    |      10        32.808

Do this:

Feet   Meters   |      Meters   Feet
----   ------   |      ------   ----
1      0.305    |      1        3.281
2      0.61     |      2        6.562
3      0.914    |      3        9.843
4      1.219    |      4        13.123
5      1.524    |      5        16.404
6      1.829    |      6        19.685
7      2.134    |      7        22.966
8      2.438    |      8        26.247
9      2.743    |      9        29.528
10     3.048    |     10        32.808

My code:

print("Feet   Meters   |      Meters   Feet\n"+ 
      "----   ------   |      ------   ----")
counter = 1
for i in range(10):
    print(counter, "    ", round(conversions.feet_to_meters(counter), 3), "   |     ", counter, "      ", round(conversions.meters_to_feet(counter), 3))
    counter += 1


So I just want to change the formatting of the answer so everything lines up, 

maybe I just have a brain fart but I can't think of a way right now.

Kop Lop
  • 29
  • 6
  • You can often get away with just using tabs instead of spaces, e.g. `print('1\t2\t3')`. – 101 Mar 04 '19 at 01:10
  • This question is already has similar answers. Check str.format() and String Formatting in Python on SO. – JChat Mar 04 '19 at 01:26

2 Answers2

1

Format strings are probably the easiest option. As an added bonus, it can do rounding for you:

for i in range(10):
    print("{0:<7}{1:<9.3f}|      {0:<7}{2:.3f}".format(counter, conversions.feet_to_meters(counter), conversions.meters_to_feet(counter)))

For instance:

print("{0:<7}{1:<9.3f}|      {0:<7}{2:.3f}".format(1, 0.3051, 3.2812))
print("{0:<7}{1:<9.3f}|      {0:<7}{2:.3f}".format(2, 0.6100, 6.5621))

Output:

1      0.305    |      1      3.281
2      0.610    |      2      6.562
iz_
  • 15,923
  • 3
  • 25
  • 40
0

I think this answer, making use of str.format(), may be what you want.

Source: python string formatting Columns in line

ggg123
  • 99
  • 10