DocsHub
PythonBeginner

Multiplication Table

Learn how to generate a multiplication table in Python using loops.

Multiplication Table

Problem

Given a number, print its multiplication table. Then extend it to print a full N×N multiplication table for any size.

Part 1 — single number table:

Input:  5
Output:
5 x 1  =  5
5 x 2  = 10
5 x 3  = 15
5 x 4  = 20
5 x 5  = 25
5 x 6  = 30
5 x 7  = 35
5 x 8  = 40
5 x 9  = 45
5 x 10 = 50

Part 2 — full N×N table:

Input:  5
Output:
    1   2   3   4   5
1   1   2   3   4   5
2   2   4   6   8  10
3   3   6   9  12  15
4   4   8  12  16  20
5   5  10  15  20  25

Logic

Part 1:

  1. Loop from 1 to 10
  2. Multiply the given number by the loop counter
  3. Print the result

Part 2:

  1. Loop through rows 1 to N
  2. For each row, loop through columns 1 to N
  3. Multiply row number by column number
  4. Print the result formatted in a grid

Flow

Yes No Start i = 1 i <= 10? Print n x i = n times i i += 1 Done

Part 2 flow:

Yes Yes No No Start row = 1 row <= N? col = 1 col <= N? Print row x col col += 1 Move to next row row += 1 Done

Part 1 — Solution 1: basic loop

Loop from 1 to 10 and multiply.

def multiplication_table(n):
    print(f"Multiplication table of {n}:")
    print("-" * 20)

    for i in range(1, 11):
        # :2d means right-align the multiplier in 2 spaces
        # :3d means right-align the result in 3 spaces
        print(f"{n} x {i:2} = {n * i:3}")


multiplication_table(5)
multiplication_table(12)

Code Execution — Part 1

Trace through multiplication_table(5):

Loopin * iPrinted
1st155 x 1 = 5
2nd2105 x 2 = 10
3rd3155 x 3 = 15
............
10th10505 x 10 = 50

range(1, 11) gives 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 — starts at 1, stops before 11.


Part 1 — Solution 2: custom range

Let the user choose the range — not just 1 to 10.

def multiplication_table(n, start=1, end=10):
    print(f"Multiplication table of {n} ({start} to {end}):")
    print("-" * 25)

    for i in range(start, end + 1):
        print(f"{n} x {i:2} = {n * i:4}")


# default range — 1 to 10
multiplication_table(7)

# custom range — 5 to 15
multiplication_table(7, start=5, end=15)

# first 5 only
multiplication_table(3, end=5)

Code Execution — Part 1 Solution 2

Trace through multiplication_table(7, start=5, end=15):

range(5, 16) gives 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

i7 * i
535
642
749
......
15105

Part 2 — Solution 1: full N×N table with nested loops

Print a complete grid — every number multiplied by every other number.

def full_multiplication_table(n):
    # print header row — column labels
    print(f"{'':4}", end="")   # empty corner cell
    for col in range(1, n + 1):
        print(f"{col:4}", end="")
    print()   # new line after header

    # print separator
    print("-" * (4 * (n + 1)))

    # print each row
    for row in range(1, n + 1):
        # print row label
        print(f"{row:<4}", end="")

        # print each column value in this row
        for col in range(1, n + 1):
            print(f"{row * col:4}", end="")

        print()   # new line after each row


full_multiplication_table(5)
print()
full_multiplication_table(10)

Code Execution — Part 2 Solution 1

Trace through full_multiplication_table(5):

Header row:

range(1, 6)1, 2, 3, 4, 5 Prints: 1 2 3 4 5

Row 1:

row = 1, range(1, 6)col = 1, 2, 3, 4, 5

colrow * colPrinted
11 × 1 = 1 1
21 × 2 = 2 2
31 × 3 = 3 3
41 × 4 = 4 4
51 × 5 = 5 5

Full row 1: 1 1 2 3 4 5

Row 3:

colrow * colPrinted
13 × 1 = 3 3
23 × 2 = 6 6
33 × 3 = 9 9
43 × 4 = 12 12
53 × 5 = 15 15

Full row 3: 3 3 6 9 12 15

end="" in print() stops Python from adding a newline after each value — so all values in a row print on the same line. Then a plain print() at the end of each row moves to the next line.


Part 2 — Solution 2: using list comprehension and join

Build each row as a list and join it into a string.

def full_multiplication_table(n):
    # header row
    header = "    " + "".join(f"{col:4}" for col in range(1, n + 1))
    print(header)
    print("-" * len(header))

    # each data row
    for row in range(1, n + 1):
        # build the row as a string
        row_str = f"{row:<4}" + "".join(f"{row * col:4}" for col in range(1, n + 1))
        print(row_str)


full_multiplication_table(5)

Code Execution — Part 2 Solution 2

Trace through row 2 of full_multiplication_table(5):

row = 2

Generator: f"{2 * col:4}" for col in range(1, 6)

col2 * colFormatted
12" 2"
24" 4"
36" 6"
48" 8"
510" 10"

"".join(...) = " 2 4 6 8 10" Full row: "2 2 4 6 8 10"


Part 2 — Solution 3: storing in a 2D list

Build the table as a 2D list first, then print it. Useful when you want to process the data before displaying.

def build_multiplication_table(n):
    # build the table as a 2D list
    # table[i][j] = (i+1) * (j+1)
    table = [
        [(row + 1) * (col + 1) for col in range(n)]
        for row in range(n)
    ]
    return table


def print_table(table):
    n = len(table)

    # header
    print(f"{'':4}", end="")
    for col in range(1, n + 1):
        print(f"{col:4}", end="")
    print()
    print("-" * (4 * (n + 1)))

    # rows
    for i, row in enumerate(table):
        print(f"{i + 1:<4}", end="")
        for value in row:
            print(f"{value:4}", end="")
        print()


table = build_multiplication_table(5)
print_table(table)

# you can also access individual values
print(f"\n3 x 4 = {table[2][3]}")   # row 3, col 4 → index [2][3]
print(f"5 x 5 = {table[4][4]}")   # row 5, col 5 → index [4][4]

Code Execution — Part 2 Solution 3

Trace through build_multiplication_table(3):

The comprehension [(row+1) * (col+1) for col in range(3)] for row in range(3):

rowcol=0col=1col=2Row result
01×1=11×2=21×3=3[1, 2, 3]
12×1=22×2=42×3=6[2, 4, 6]
23×1=33×2=63×3=9[3, 6, 9]

Result: [[1,2,3], [2,4,6], [3,6,9]]

Accessing table[2][3] means row index 2 (= row 3), col index 3 (= col 4) → 3 × 4 = 12


Which solution to use?

SolutionHowBest when
Part 1 — Solution 1Basic loopLearning loops
Part 1 — Solution 2Custom rangeFlexible, real use
Part 2 — Solution 1Nested loopsUnderstanding nested iteration
Part 2 — Solution 2List comprehension + joinCleaner code
Part 2 — Solution 32D listWhen you need to store and process the data

Output

Part 1 — multiplication_table(5):

Multiplication table of 5:
--------------------
5 x  1 =   5
5 x  2 =  10
5 x  3 =  15
5 x  4 =  20
5 x  5 =  25
5 x  6 =  30
5 x  7 =  35
5 x  8 =  40
5 x  9 =  45
5 x 10 =  50

Part 2 — full_multiplication_table(5):

       1   2   3   4   5
--------------------
1      1   2   3   4   5
2      2   4   6   8  10
3      3   6   9  12  15
4      4   8  12  16  20
5      5  10  15  20  25

On this page