Some people pointed out your question may be a duplicate, but the answers in question look somewhat old... Since the introduction of the new string format mini-language, you can do this kind of thing using str.format
alone.
Supposing your list looks like:
data = [
["John" , 15.00, 1.20, 10.00, 8.00],
["Robert", 45.00, 6.70, 24.00, 14.00],
["Duncan", 10.00, 5.40, 5.00, 10.00],
["Steven", 30.00, 6.50, 30.00, 16.00],
]
You can use the format method:
# Headers, centered
print("{: ^15s} {: ^15s} {: ^15s} {: ^15s} {: ^15s}".format(
"Name", "Age", "Height", "Pay Rate", "Grade")
)
# Dashes (not very readable, but I'm lazy)
print(" ".join([("-" * 15)] * 5))
# Lines
for line in data:
print("{:15s} {: 15.2f} {: 15.2f} {: 15.2f} {: 15.2f}".format(*line))
The format specification is (see the format specification mini-language):
format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
fill ::= <any character>
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::= integer
precision ::= integer
type ::= (pretty much like printf)
^
means centered.
Result should be like:
Name Age Height Pay Rate Grade
--------------- --------------- --------------- --------------- ---------------
John 15.00 1.20 10.00 8.00
Robert 45.00 6.70 24.00 14.00
Duncan 10.00 5.40 5.00 10.00
Steven 30.00 6.50 30.00 16.00
Making this shorter and more reusable is left as an exercise for you.