Write Python. Understand every error.

Open the editor

Python programs · Numbers

Python program to print a multiplication table

A multiplication table is a neat first use of a for loop and of f-strings.

Approach

  1. Read the number.
  2. Loop from 1 to 10.
  3. Print each line with the number, the multiplier and the product.

The program

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

Sample run

Input
7
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

Run this program in Py-CRAFT

How the code works

range(1, 11) gives 1 to 10 because the stop value is not included. Inside the f-string, anything in curly brackets is worked out, so {n * i} prints the product. To line up the columns for larger tables, use a width such as {n * i:3}.

Try changing it

Common mistakes

Learn the ideas

More programs

Share this page: