Python Program to Print a Multiplication Table
i from 1 to 10 with range(1, 11) and print n * i for each step, for example print(f"{n} x {i} = {n * i}").Printing a times table is one of the best beginner exercises for loops: the work is repeated, the output is easy to check, and small changes (a different range, a different layout) teach you a lot about for loops and formatting.
Below you will print the table for one number, make the range flexible, and then build a full grid with nested loops. Lined-up columns use f-strings, which are far easier than concatenating text.
| Input | A number n (and optionally how many rows) |
|---|---|
| Output | Lines such as 7 x 3 = 21 |
| Approach | A for loop over range(1, 11) multiplying n by the loop variable |
| Time complexity | O(rows), or O(rows x columns) for a grid |
Approach (step by step)
- Choose the number
nwhose table you want. - Loop
iover the multipliers 1 to 10. - On each pass, print the multiplication in a readable format.
The only trap is the end of the range. range(1, 11) produces 1 through 10 because the stop value is excluded; see range(). If you want the table to 12, use range(1, 13).
How the code works
The loop variable i takes each value from the range, and the f-string inserts n, i and n * i into the line. Everything inside the loop body is indented, and n itself never changes, only i does.
For aligned output, use format specs inside the braces: {i:>2} right-aligns a value in a field two characters wide, so 9 and 10 line up. This matters even more for the grid, where every cell needs the same width.
n = 5
for i in range(1, 4):
print(f"{n} x {i:>2} = {n * i:>3}")Variations: custom range, while loop and full table grid
You can let the user decide where the table ends by turning the 10 into a variable. A while loop works as well; it just needs a counter that you must increase yourself, which is the usual source of infinite loops.
A full multiplication grid uses a loop inside a loop: the outer loop picks the row, the inner loop picks the column, and each cell is row * col. Printing with end="" keeps a row on one line, and an empty print() ends the row.
| Goal | Code | Notes |
|---|---|---|
| Table of 7 up to 10 | for i in range(1, 11) | Stop value 11 is excluded |
| Table up to 12 | for i in range(1, 13) | Change only the stop value |
| Even rows only | for i in range(2, 11, 2) | Third argument is the step |
| Count down | for i in range(10, 0, -1) | Negative step |
Examples you can run
Press Run, then edit the code and run it again.
Multiplication table of one number
The loop prints ten lines. The value of n could just as well come from input().
n = 7
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")Expected output7 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
Full grid with nested loops
Each row is built with an inner loop. {value:>4} right-aligns every cell in four characters.
for row in range(1, 5):
for col in range(1, 6):
print(f"{row * col:>4}", end="")
print()Expected output 1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
Interactive version with a chosen number of rows
Both the number and the length of the table are read from the user.
n = int(input("Table of: "))
rows = int(input("How many rows: "))
for i in range(1, rows + 1):
print(f"{n} x {i} = {n * i}")Expected outputTable of: 12
How many rows: 3
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
Common mistakes
Using range(1, 10) and losing the last row
✗ Wrong
n = 7
for i in range(1, 10):
print(n, "x", i, "=", n * i)✓ Right
n = 7
for i in range(1, 11):
print(n, "x", i, "=", n * i)Use range(1, 11) to include 10.
Multiplying by a string from input()
✗ Wrong
n = input("Table of: ")
for i in range(1, 4):
print(n * i)✓ Right
n = int(input("Table of: "))
for i in range(1, 4):
print(n * i)input() returns text, and text times an int repeats it. Convert with int().
Python Program to Print a Multiplication Table: questions people ask
How do you print a multiplication table in Python?
Use a for loop: for i in range(1, 11): print(f"{n} x {i} = {n * i}"). The loop runs for i = 1 to 10.
How do I print the multiplication table up to 12 in Python?
Change the range to range(1, 13), because the stop value of range() is never included.
How do I print a full multiplication table grid in Python?
Use two nested for loops: the outer loop for the rows and the inner one for the columns, printing row * col with end="" and an empty print() after each row.
Keep learning
Step-by-step lessons:
Python for LoopsPython Nested LoopsPython f-Strings
Related:
Python range() FunctionPython Star Pattern Programs (Triangle, Pyramid, Diamond)Python FizzBuzz ProgramPython Simple Calculator Program
More in number programs:
Python Program to Check Even or Odd NumberPython Program to Check Prime NumberPython Program to Find Factorial of a NumberPython Program to Print the Fibonacci SeriesPython Program to Check Armstrong NumberPython Program to Find GCD of Two NumbersPython Program to Check Leap YearPython Program to Find the Sum of Digits of a Number See all →