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
- Read the number.
- Loop from 1 to 10.
- 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
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
- Ask for the number of rows as well as the number.
- Print the tables from 1 to 10 with a loop inside a loop.
Common mistakes
- Using
range(1, 10), which stops at 9. - Writing
n x iinside the quotes without curly brackets, which prints the letters instead of the values.