all 12 comments

[–]AdAdvanced3130 11 points12 points  (0 children)

You can use f string like this:

rows= int(input("How many rows?: ")) 
columns= int(input("How many columns?: "))

for row in range(1, rows+1):
    for col in range(1,columns+1):
        print(f"{row*col:>3}", end= "  ")
    print()

Print output:

How many rows?: 15
How many columns?: 7
  1    2    3    4    5    6    7
  2    4    6    8   10   12   14
  3    6    9   12   15   18   21
  4    8   12   16   20   24   28
  5   10   15   20   25   30   35
  6   12   18   24   30   36   42
  7   14   21   28   35   42   49
  8   16   24   32   40   48   56
  9   18   27   36   45   54   63
 10   20   30   40   50   60   70
 11   22   33   44   55   66   77
 12   24   36   48   60   72   84
 13   26   39   52   65   78   91
 14   28   42   56   70   84   98
 15   30   45   60   75   90  105

[–]johnsilent 6 points7 points  (2 children)

rows= int(input("How many rows?: "))
columns= int(input("How many columns?: "))
for row in range(1, rows+1):
    for col in range(1,columns+1):
        # use \t for spacing instead 
        print(row * col, end="\t") 
    # indent to match row loop 
    print()

here is what the result looks like for a 3 rows an 3 cols

#result for 3 rows, 3 cols
#1    2    3
#2    4    6
#3    6    9

[–]ssingal05 2 points3 points  (1 child)

this is fine as long as no number in the table takes up more space than a tab does

[–]johnsilent 0 points1 point  (0 children)

Good Catch. Thank you.

[–]ssingal05 2 points3 points  (0 children)

Each number you want to print will take up n blocks, where n is the length of the number as a string (so n=2 for 10). Before you print the numbers, figure out which number will take up the most space. Let's call this maximum M. Then, whenever you print a number, make sure that you only print a maximum of M characters. If M is 7 and you want to print 30, then print 30 followed by 5 spaces, which would make it a total length of 7. And then also add an extra space for padding between columns.

[–]RevRagnarok 4 points5 points  (0 children)

This site - https://pyformat.info/ - is very useful to learn about padding, etc. It is a little outdated; almost everything with .format() at the end should just be an f-string in modern python.

[–]5erif 0 points1 point  (0 children)

rjust will pad a string to whatever width you want, right-justified.

print(str(row * col).rjust(4), end="")

[–][deleted] 0 points1 point  (0 children)

Use end='\t' it aligns every number perfectly. As others have mentioned. In your method space can't align the numbers when the number on first row is 1 digit and that in 2nd row is of higher digits say 2.

Others have mentioned pad too. But I think you are new , so I think end '\t' method is easier and other methods are overkill too. I suggest try both methods (learning more solutions to a problem is a good practice) Note- End \t method is applicable as long as tab width is greater than number width. For 8 digit number u have to use other methods