PythonMastery

Multiplication table program in Python

Ask for a number and print its multiplication table from 1 to 10 with a for loop, with the columns lined up. Also prints a full 1 to 5 grid.

A for loop over range(1, 11) gives the numbers 1 to 10. Multiply each one by the chosen number and print the line.

The program

python
n = int(input("Table of: "))

for i in range(1, 11):
    print(f"{n} x {i:2} = {n * i:3}")

Typed in this example: 7. Press Run to type your own.

output
Table of: 7
7 x  1 =   7
7 x  2 =  14
7 x  3 =  21
7 x  4 =  28
7 x  5 =  35
7 x  6 =  42
7 x  7 =  49
7 x  8 =  56
7 x  9 =  63
7 x 10 =  70

How it works

A full table grid

A loop inside another loop prints a grid. The outer loop picks the row, and the inner loop prints each column of that row on the same line.

python
size = 5

for row in range(1, size + 1):
    for col in range(1, size + 1):
        print(f"{row * col:4}", end="")
    print()
output
   1   2   3   4   5
   2   4   6   8  10
   3   6   9  12  15
   4   8  12  16  20
   5  10  15  20  25

end="" stops print() from moving to a new line after each number. The empty print() after the inner loop ends the row.

Learn the idea: Loops: for, while, and the Pythonic Patterns, F-Strings: Modern String Formatting