Multiplication Table in Python
Print 7 x 1 = 7, 7 x 2 = 14 and onward with a for loop and an f-string.
- Looping with range()
- Building each row with an f-string
- Aligning columns
- 1
7 x 1 = 7 - 2
7 x 2 = 14 - 3
7 x 3 = 21 - 4
7 x 5 = 35
One row per multiplier
The problem
Printing a multiplication table is one of the first programs that produces many lines of output from one rule. It shows how a loop plus a format string replaces dozens of nearly identical print statements.
Think of it like this
A loop that prints a table is like a stamp with a blank space. The stamp always prints 7 x _ = _, and for every row you fill the blanks with the next multiplier and its product.
number = 7🔢 The table you wantfor multiplier in range(1, 6):🔁 One row for each multiplier 1 to 5f"{number} x {multiplier} = {number * multiplier}"🖨️ Stamp one row with the blanks filledprint(...)📃 Each row on its own lineA multiplication table lists the products of one number with a series of multipliers, one row per multiplier.
Key points
range(1, 6)gives the multipliers 1 to 5.number * multiplieris computed inside the braces of the f-string.- Each loop round prints one row, so five rows appear.
- Changing
numberor the range gives a different table without changing anything else.
When should you use it?
- Generating repeated, patterned output from one rule.
- Practicing
forloops withrangeand f-strings. - Printing simple reports and tables.
When not to use it
- Building a table of data to use later: store the values in a list instead of printing them.
- Wide tables: align columns with format specifiers such as
{product:>3}.
Example code
number = 7
# Generate one formatted row for each multiplier.
for multiplier in range(1, 6):
print(f"{number} x {multiplier} = {number * multiplier}")
Result of running the code
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
Code explanation
range(1, 6) produces the multipliers 1, 2, 3, 4 and 5, one for each loop round. In every round the f-string inserts number, the current multiplier and their product, computed directly inside the braces. Because print() is inside the loop, each product appears on its own line.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Missing the last row
range(1, 5) stops at 4.
for multiplier in range(1, 5):
for multiplier in range(1, 6):
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Multiplication Table Row List the first multiples of a number. Easy
- Multiplication Grid Build a full multiplication table. Easy
- Countdown to Liftoff Count down from a number and end with Liftoff! Easy