Python Forum
Formatted table with headings - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Formatted table with headings (/thread-15981.html)



Formatted table with headings - KameronG - Feb-08-2019

Hello,
I'm trying to have a program print the results from a formula into a formatted table, but I can't figure out how to get it to attach heading labels to the rows so it's clear what value is which. I have:

if cs == "1":
    dA = d1
    redshift = z
    dL = d1*(1 + z)**2
    print(f"{redshift:13.3f} {dA:13.3f} {dL:13.3f}")
else:
    dA = d2
    redshift = z
    dL = d2*(1 + z)**2
    print(f"{redshift:13.3f} {dA:13.3f} {dL:13.3f}")
which gives me:

Output:
0.500 5.407 12.165
I want it to say redshift, dA, and dL above the numbers but I can't find out how to do that anywhere.


RE: Formatted table with headings - Larz60+ - Feb-09-2019

you should always post enough of a code snippet so that it can be run, otherwise we can only approximate the proper answer
assuming fields of 17 characters each:
head = ['redshift', 'dA', 'dL']
print(f'{head[0]:17s} {head[1]:17s} {head[2]:17s}')