Formatting multiplication tables in python; not how to, just some explanation

33 Views Asked by At

I'm meant to make a multiplication table in Python using nested loops. I had trouble formatting it to make a table and looked for answers online. I came across a solution:

uplim=int(input("What is the upper bound of the multiplication table? "))
for row in range(1, uplim+1):
    print(*("{:3}".format(row*col) for col in range(1, uplim+1)))

and this solution works and formats the printed table correctly. I wanted to know why it works, specifically - *("{:3}" - what is that part for and what does it do?

I originally attempted :

for row in range(1, uplim+1):
    for col in range(1, uplim+1):
        print(row*col)

but it only printed one long column.

Additionally, my teacher's instructions say this:

NOTE: The format function can be used to align your output. For instance, see the example below. '6d' means width of the value is 6 digits and type 'd' means integer digit. '6,d' means 6 digit integer with a , for every third digit from the right.

>>> z = 1234
>>> print(f'{z: 6d}')
'  1234'
>>> print(f'{z: 6,d}')
'  1,234'

but I'm just not sure what to type in.

0

There are 0 best solutions below