# Exercise #25: Multiplication Table

Learning the multiplication table is an early part of our childhood math education. The multiplication table shows every product of two single digit numbers. In this exercise, we print a multiplication table on the screen using nested `for` loops and some string manipulation to align the columns correctly.

Exercise Description

Write a program that displays a multiplication table that looks like this:

| 1  2  3  4  5  6  7  8  9 10

--+------------------------------

1| 1  2  3  4  5  6  7  8  9 10

2| 2  4  6  8 10 12 14 16 18 20

3| 3  6  9 12 15 18 21 24 27 30

4| 4  8 12 16 20 24 28 32 36 40

5| 5 10 15 20 25 30 35 40 45 50

6| 6 12 18 24 30 36 42 48 54 60

7| 7 14 21 28 35 42 49 56 63 70

8| 8 16 24 32 40 48 56 64 72 80

9| 9 18 27 36 45 54 63 72 81 90

10|10 20 30 40 50 60 70 80 90 100

The number labels along the top and left sides are the numbers to multiply, and where their column and row intersect is the product of those numbers. Notice that the single-digit numbers are padded with spaces to keep them aligned in the same column. You may use Python’s `rjust()` string method to provide this padding. This method returns a string with space characters added on the left side to right-justify the text, and the Solution Design section explains how it works.

The line along the top side of the table is made up of minus sign characters. The line along the left side is made up of vertical pipe characters (above the Enter key on the keyboard). A plus sign marks their intersection. Your solution is correct if the output matches the above text of the multiplication table. You can use a simple `print()` call for the number labels and lines at the top of the table. However, don’t hard code the text of the multiplication table into your program: your program should be more than just a bunch of `print()` calls.

Try to write a solution based on the information in this description. If you still have trouble solving this exercise, read the Solution Design and Special Cases and Gotchas sections for additional hints.

Prerequisite concepts: `print()`, for loops, `range()` with two arguments, `end` keyword argument for print(), `rjust()`, str()

Solution Design

In the first part of the program, print out the horizontal number labels and separating line. You can program these two lines directly with two print() calls:

print('  | 1  2  3  4  5  6  7  8  9 10')

print('--+------------------------------')

Remember that you need the appropriate amount of spaces in between the numbers so the columns of the multiplication table to line up. You can treat all numbers as though they were two digits. The single-digit numbers should have a space printed on their left side, making the string right-justified. Python’s `rjust()` string method can do this for you. Enter the following into the interactive shell:

>>> '42'.rjust(4)  # Adds two spaces.

'  42'

>>> '042'.rjust(4)  # Adds one space.

' 042'

>>> '0042'.rjust(4)  # Adds zero spaces.

'0042'

Notice how all of the strings returned from the rjust(4) call are four characters long. If the original string is less than four characters long, the `rjust()` method puts spaces on the left side of the returned string until it is four characters long.

To print out the multiplication table, two nested for loops can iterate over each product. The outermost for loop iterates over the numbers of each row, and the innermost `for` loop iterates over the numbers of each column in the current row. You don’t want a newline to appear after each product, but only after each row of products. Python’s `print()` function automatically adds a newline to the end of the string you pass. To disable this, pass a blank string for the `end` keyword argument like `print('Some text', end='')`.

A simplified version of this code would look like this:

>>> for row in range(1, 11):

...     for column in range(1, 11):

...         print(str(row * column) + ' ', end='')

...     print()  # Print a newline.

...

1 2 3 4 5 6 7 8 9 10

2 4 6 8 10 12 14 16 18 20

3 6 9 12 15 18 21 24 27 30

4 8 12 16 20 24 28 32 36 40

5 10 15 20 25 30 35 40 45 50

6 12 18 24 30 36 42 48 54 60

7 14 21 28 35 42 49 56 63 70

8 16 24 32 40 48 56 64 72 80

9 18 27 36 45 54 63 72 81 90

10 20 30 40 50 60 70 80 90 100

Your solution needs these products appropriately aligned as well as the number labels along the top and left side.

Special Cases and Gotchas

The easiest mistake is getting the amount of padding wrong, causing the columns to no longer be aligned. For example, if you fail to pad the single-digit products with an extra space character, the multiplication table ends up looking like the misaligned table in the previous section.

Now try to write a solution based on the information in the previous sections. If you still have trouble solving this exercise, read the Solution Template section for additional hints.

Solution Template

Try to first write a solution from scratch. But if you have difficulty, you can use the following partial program as a starting place. Copy the following code from https://invpy.com/multiplicationtable-template.py and paste it into your code editor. Replace the underscores with code to make a working program:

# Print the heading of each column:

print('  | 1  2  3  4  5  6  7  8  9 10')

print('--+------------------------------')

# Loop over all numbers from 1 to 10:

for column in range(____, ____):

# Print the number label on the right side:

print(str(____).rjust(____) + '|', end=____)

# Loop over all numbers from 1 to 10:

for row in range(____, ____):

# Print the product, padded to two digits, followed by a space:

print(str(____).rjust(____) + ' ', end=____)

# After the loop, print a newline to end the row:

____()

The complete solution for this exercise is given in Appendix A and https://invpy.com/multiplicationtable.py. You can view each step of this program as it runs under a debugger at https://invpy.com/multiplicationtable-debug/.

Further Reading

A similar multiplication table project is also used in the free book, The Big Book of Small Python Projects, as Project #49 at https://inventwithpython.com/bigbookpython. If you are interested in chemistry, that book also has a project that displays the periodic table of elements.