Editor

Python Program to Print a Multiplication Table

Beginner · Basics

A multiplication table lists a number multiplied by 1, 2, 3 and so on, usually up to 10. This program prints the table of 7 using a for loop with range().

Program

multiplication_table.py
num = 7 # or: num = int(input("Enter a number: "))
print("Multiplication table of", num)
for i in range(1, 11):
print(num, "x", i, "=", num * i)
Output
Multiplication 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

  1. range(1, 11) gives the numbers 1 to 10, because the end value 11 is not included.
  2. On each pass of the loop, i takes the next value and num * i is the product.
  3. print() puts a space between its items, giving lines like 7 x 3 = 21.

Other ways to write it

Using a while loop

Using a while loop
num = 7
i = 1
while i <= 10:
print(f"{num} x {i:2} = {num * i:3}")
i = i + 1
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

The :2 and :3 in the f-string pad the numbers to a fixed width, so the columns line up.

Tables of 1 to 5 in a grid

Tables of 1 to 5 in a grid
for num in range(1, 6):
for i in range(1, 11):
print(f"{num * i:4}", end="")
print() # move to the next line after each table
Output
   1   2   3   4   5   6   7   8   9  10
   2   4   6   8  10  12  14  16  18  20
   3   6   9  12  15  18  21  24  27  30
   4   8  12  16  20  24  28  32  36  40
   5  10  15  20  25  30  35  40  45  50

A loop inside a loop prints one row per table, and end="" keeps the products of one table on the same line.

Try it yourself

Print the table of 12 from 12 x 1 up to 12 x 20.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises