How to print lists to a nice table?

Hi guys, I got this table here. I’m pretty new to python so I’m still learning but I have been trying to remove the row on the side that repeat (Place, Name, Time) without any luck. Been trying now for about 30 minutes without luck, all I get is errors. Thanks in advance if someone can help me!

Right now it looks like this:

           Place          Name        Time        
Place      1              xxx         99.999      
Name       2              xxx         99.999      
Time       3              xxx         99.999

I want the table to look like it is doing below, without any spaces in the beginning and for it to line up with the text under beatifully.

Place         Name        Time        
1             xxx         99.999      
2             xxx         99.999      
3             xxx         99.999

My code this far:

user1 = "xxx"
user2 = "xxx"
user3 = "xxx"
time1 = 99.999
time2 = 99.999
time3 = 99.999


header = ["Place", "Name", "Time"]
data = [[1, user1, time1],
        [2, user2, time2],
        [3, user3, time3]]
    
format_row = "{:<12}" * (len(header) + 1)

print(format_row.format("", *header))
for header, row in zip(header, data):
    print(format_row.format(header, *row))

You’re printing four columns when you seem to want three. Would changing your format row to just len(header) removing the "" from the first print and removing header from the second print be closer to what you’re aiming for?

Do you mean like this?

user1 = "xxx"
user2 = "xxx"
user3 = "xxx"
time1 = 99.999
time2 = 99.999
time3 = 99.999


header = ["Place", "Name", "Time"]
data = [[1, user1, time1],
        [2, user2, time2],
        [3, user3, time3]]
    
format_row = (len(header))

print(format_row.format(*header))

for row in zip(data):
    print(format_row.format(*row))

I get the error ‘int’ object has no attribute ‘format’

It seems closer but you removed some code which was giving you your nice padding. Perhaps my previous comment should have suggested to alter len(x) + 1 to just len(x). You need the formatting option and the number of repeats in this style:

print('a' * 3)
Out: aaa

Check to see what you cut out from your original code.

If you wanted to know exactly how this formatting works with padding then check the docs for the formatting syntax options- https://docs.python.org/3/library/string.html#format-specification-mini-language

1 Like

I got it to work now with:

user1 = "xxx"
user2 = "xxx"
user3 = "xxx"
time1 = 99.999
time2 = 99.999
time3 = 99.999


header = ["Place", "Name", "Time"]
data = [[1, user1, time1],
        [2, user2, time2],
        [3, user3, time3]]
    
format_row = "{:<12}" * (len(header))

print(format_row.format(*header))
for header, row in zip(header, data):
    print(format_row.format(*row))

Thank you so much for the help, can’t believe it was this easy and I tried for an hour. I will read some more on how the formatting works but I think I get it now! Thanks again!