Home / Python / Example programs / Loops and numbers

Python program: Multiplication table

Print the multiplication table of a number.

The code

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

Output

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

range(1, 11) gives 1 to 10. The widths :2 and :3 keep the columns lined up.

Python features used

Learn the ideas behind it in the free lesson Loops.