Multiplication Table Program in Python
Print the multiplication table of a number in Python using a for loop with formatted output.
- Time
O(k) for one table of k lines; O(r * t) for r rows of t tables - Space
O(1); O(t) for the row list in the side-by-side version - Hints3
- Read4 min
To print a multiplication table in Python, loop with for i in range(1, 11) and print f"{n} x {i} = {n * i}". range(1, 11) gives 1 to 10, because the stop value 11 is not included. Add a width such as {i:2} and {n * i:3} inside the f-string to line up the columns. Take n from the user with int(input()).
Problem statement
Write a multiplication table program in Python. Given a number n, print its
table from 1 to 10 in the form n x i = result.
Use a for loop, and line up the output so the = signs and results form neat
columns.
An integer n.
Ten lines, each in the form "n x i = n*i".
Examples
n = 77 x 1 = 7 ... 7 x 10 = 70Ten lines, with i and the result padded so the columns line up.
n = 12, up to 512 x 1 = 12 ... 12 x 5 = 60The loop limit is taken from the user instead of being fixed at 10.
Constraints
- n is an integer (it can be negative or 0)
- The table runs from 1 to 10 unless the question gives another limit
Think before you code
Reveal the questions to ask yourself first
- Which values should i take, and what range() call gives exactly those?
- What changes from one line to the next, and what stays the same?
- How do you stop 7 x 10 = 70 from pushing the columns out of line?
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
Approach
Reveal the step-by-step approach
For loop table in Python
- Read or set
n. - Loop
ioverrange(1, 11). - On each pass, print
n,iandn * i.
Formatting the output
| Code | Prints for i = 1 | Prints for i = 10 |
|---|---|---|
print(n, "x", i, "=", n * i) |
7 x 1 = 7 |
7 x 10 = 70 |
print(f"{n} x {i} = {n * i}") |
7 x 1 = 7 |
7 x 10 = 70 |
print(f"{n} x {i:2} = {n * i:3}") |
7 x 1 = 7 |
7 x 10 = 70 |
{i:2} means "use at least 2 characters for i". Numbers are right-aligned by default,
so 1 gets a space in front and lines up under 10. {n * i:3} does the same for the
result. If loops are new to you, read loops in programming.
Dry run
Walk through the example step by step
n = 7, first four passes:
i | n * i | printed line
--+-------+---------------
1 | 7 | 7 x 1 = 7
2 | 14 | 7 x 2 = 14
3 | 21 | 7 x 3 = 21
4 | 28 | 7 x 4 = 28
The loop continues up to i = 10, then range(1, 11) runs out and the loop ends.
Solution
Reveal the full Python solution
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
The loop body runs 10 times. Each line uses the same n and a new i.
Print table of a number in Python with user input
n = int(input("Enter a number: "))
upto = int(input("Up to: "))
for i in range(1, upto + 1):
print(f"{n} x {i} = {n * i}")
Enter a number: 12
Up to: 5
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
input() returns text, so int() turns it into a number. Without int(),
n * i would repeat the text: "12" * 3 is "121212". The stop value is
upto + 1 so that upto itself is included.
Multiplication table in Python using a while loop
n = 9
i = 1
while i <= 10:
print(n, "x", i, "=", n * i)
i += 1
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90
The while loop needs three things you must write yourself: the start i = 1, the
condition i <= 10, and the step i += 1. Forget the step and the loop never ends.
print() with commas adds one space between items, which is why the last line is
not aligned.
Tables 2 to 5 side by side with nested loops
for i in range(1, 11):
row = []
for n in range(2, 6):
row.append(f"{n} x {i:2} = {n * i:2}")
print(" ".join(row))
2 x 1 = 2 3 x 1 = 3 4 x 1 = 4 5 x 1 = 5
2 x 2 = 4 3 x 2 = 6 4 x 2 = 8 5 x 2 = 10
2 x 3 = 6 3 x 3 = 9 4 x 3 = 12 5 x 3 = 15
2 x 4 = 8 3 x 4 = 12 4 x 4 = 16 5 x 4 = 20
2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30
2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35
2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40
2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45
2 x 10 = 20 3 x 10 = 30 4 x 10 = 40 5 x 10 = 50
The outer loop picks the row (i), and the inner loop walks across the tables
(n). Each row is collected in a list and joined with three spaces.
For more loop practice, see number pattern programs in Python and pyramid pattern program in Python.
O(k) for one table of k lines; O(r * t) for r rows of t tablesO(1); O(t) for the row list in the side-by-side versionReview before the interview
Common mistakes
- Using range(1, 10), which stops at 9 and misses the 10th line.
- Forgetting int() around input(), so "12" * 3 prints 121212 instead of 36.
- Forgetting i += 1 in the while version, which makes an infinite loop.
- Writing range(10), which starts at 0 and prints 7 x 0 = 0 first.
Edge cases to test
- n = 0: every result is 0.
- Negative n, like -3: results are negative; the code does not change.
- Up to 0 in the input version: range(1, 1) is empty, so nothing is printed.
- Large n: widths like {n * i:3} are too small, so raise the width or drop it.
Interview follow-ups
- How would you print the table in reverse, from 10 down to 1?
- How would you print a full 10 x 10 multiplication grid?
- How would you save the table to a text file instead of printing it?
Keep practising
Number Pattern Programs in Python with Output
Hints · Dry run · SolutionSolve Similar problemPyramid Pattern Program in Python
Hints · Dry run · SolutionSolve Similar problemFizzBuzz Problem
Hints · Dry run · SolutionSolve Similar problemLeap Year Program in Python with Output
Hints · Dry run · SolutionSolve Related learningLoops In Programming
TutorialLearn Related learningWhat Is Python Used For
TutorialLearn Related learningPython Interview Questions
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartPractising Python for interviews?
CodeBegun’s Python Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
