# Print a Multiplication Table in Python Using Basic Programming Concepts

We can practice several elementary programming concepts by learning to print a times table in Python. These include:

- Using variables
- Getting user input
- Using inbuilt functions
- Type casting variables
- Iteration (loop)
- String formatting
- Using a Unicode symbol

We will use Python’s `f`

string formatting feature, available for Python 3.6 and above.

## Basic Programming Concepts

We can declare a variable and assign it a value as follows.

```
table_of = 5
```

We will use the `input()`

function to get user input, as shown below.

```
table_of = input("Print times table of: ")
```

The program will display the string `Print times table of:`

and wait for user input. The user can input anything. Python interprets the input as a string.

To convert it to an integer, we will use the `int()`

function around the `input()`

function.

```
table_of = int(input("Print times table of: "))
```

`print("Times")`

prints the word `Times`

on the display. An empty `print()`

function prints an empty line.

The `range()`

function creates a sequence from `start_int`

to, but excluding, `end_int`

. By default, it increases by 1.

```
range(start_int, end_int, step_int)
```

We will use the `for`

loop in our code. It repeats the code in the loop as many times as the variable is in the specified range.

```
for variable in range(start, end):
code to repeat
```

Python’s `f`

string formatting feature allows us to include variables in strings using placeholders `{}`

. To use the value of the variable `table_of`

, we will use:

```
print(f"Times table of {table_of}")
```

We can specify the length of the placeholder using an integer. In the code, we specify this using another variable: the length of the result `table_of * 9`

.

We convert the integer to a string using `str()`

to get the length.

The multiplication symbol is specified using its Unicode name.

```
\N{MULTIPLICATION SIGN}
```

## Print the Times Table of a Given Number in Python

We will now put all the above concepts in the following code. It will print the multiplication table of the user-given number in two ways.

Example Code:

```
# The following code prints the times table
# of the given number till 'number x 9'.
# It prints the times table in two different ways.
table_of = int(input("Print times table of: "))
# Get the length of the result
l_res = len(str(table_of*9))
print(f"Times Table of {table_of}:")
print()
for multiple in range(1, 10):
print(f'{multiple} \N{MULTIPLICATION SIGN} {table_of} = {table_of*multiple:{l_res}}')
print()
print("-------------")
print()
for multiple in range(1, 10):
print(f'{table_of} \N{MULTIPLICATION SIGN} {multiple} = {table_of*multiple:{l_res}}')
print()
```

Sample Output:

```
Print times table of: 1717
Times Table of 1717:
1 × 1717 = 1717
2 × 1717 = 3434
3 × 1717 = 5151
4 × 1717 = 6868
5 × 1717 = 8585
6 × 1717 = 10302
7 × 1717 = 12019
8 × 1717 = 13736
9 × 1717 = 15453
-------------
1717 × 1 = 1717
1717 × 2 = 3434
1717 × 3 = 5151
1717 × 4 = 6868
1717 × 5 = 8585
1717 × 6 = 10302
1717 × 7 = 12019
1717 × 8 = 13736
1717 × 9 = 15453
```

As a variation, we can print the multiplication table from and to a desired multiple of the given number.

Example Code:

```
# The following code prints the times table
# of the given number from a multiple till a multiple.
table_of = int(input("Print times table of: "))
# We will assume that the user correctly gives a smaller number
# at which to start and a larger number at which to end.
from_multiple = int(input("Enter the multiple at which to start: "))
to_multiple = int(input("Enter the multiple at which to end: "))
# Get the length of the result
l_res = len(str(table_of*to_multiple))
# Get the length of the larger multiple.
l_multiple = len(str(to_multiple))
print(f"Times Table of {table_of}:")
print()
for multiple in range(from_multiple, to_multiple+1):
print(f'{multiple:{l_multiple}} \N{MULTIPLICATION SIGN} {table_of} = {multiple*table_of:{l_res}}')
print()
```

Sample Output:

```
Print times table of: 16
Enter the multiple at which to start: 5
Enter the multiple at which to end: 15
Times Table of 16:
5 × 16 = 80
6 × 16 = 96
7 × 16 = 112
8 × 16 = 128
9 × 16 = 144
10 × 16 = 160
11 × 16 = 176
12 × 16 = 192
13 × 16 = 208
14 × 16 = 224
15 × 16 = 240
```