Skip to content
Devtoolry

Patterns and small projects · beginner

Python Program to Display the Multiplication Table

Print the multiplication table of any number in Python with a for loop and aligned f-strings, then a full 1-to-10 grid with nested loops.

Printing a multiplication table is a simple loop, but it is also a good place to learn f-string formatting: {value:>3} right-aligns a value in three characters, which keeps the columns straight.

This program reads the number from standard input, so put a number in the stdin box before you run it. The second version below prints a whole grid with two nested loops, the first step towards the pattern programs.

The program

Python
n = int(input())

for i in range(1, 11):
    print(f"{n} x {i:>2} = {n * i:>3}")
Input (stdin)
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

How it works

  1. int(input()) reads the number from the stdin box (7 here) and converts it to an integer. With an empty stdin box, input() has nothing to read and raises EOFError.
  2. range(1, 11) gives 1 to 10; the end value 11 is not included.
  3. {i:>2} right-aligns i in two characters, and {n * i:>3} right-aligns the product in three, so all rows line up.

Watch it run line by line

Only i changes on each pass, while n stays 7. Watch the output grow one line per step.

“Visualize step by step” opens the program in the Python Compiler and records every step in your browser. Use the arrow keys to move forwards and backwards; changed variables are highlighted.

Another way: A full 1-to-10 grid

Two nested loops print the complete table. The inner loop builds one row, and end="" keeps it on one line.

Python
for row in range(1, 6):
    for col in range(1, 6):
        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

Common mistakes

  • Using range(10), which runs from 0 to 9 and prints "7 x 0 = 0" but not "7 x 10".
  • Forgetting int() around input(): "7" * 3 is "777", so the table prints repeated text instead of products.
  • Building the whole table in one long string with + inside the loop. It works, but printing each row as you go is simpler and easier to debug.

Try it yourself

  • Print the table up to a limit that is also read from input.
  • Extend the grid to 12 × 12 and add a header row.

Open the program in the Python Compiler, change it and press Run: there is nothing to install, and your code never leaves your browser.

Browse all 25 Python examples, or write your own program in the online Python Compiler.