Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Properly formatted multiplication table

Writer Mia Lopez

How would I make a multiplication table that's organized into a neat table? My current code is:

n=int(input('Please enter a positive integer between 1 and 15: '))
for row in range(1,n+1): for col in range(1,n+1): print(row*col) print()

This correctly multiplies everything but has it in list form. I know I need to nest it and space properly, but I'm not sure where that goes?

19 Answers

Quick way (Probably too much horizontal space though):

n=int(input('Please enter a positive integer between 1 and 15: '))
for row in range(1,n+1): for col in range(1,n+1): print(row*col, end="\t") print()

Better way:

n=int(input('Please enter a positive integer between 1 and 15: '))
for row in range(1,n+1): print(*("{:3}".format(row*col) for col in range(1, n+1)))

And using f-strings (Python3.6+)

for row in range(1, n + 1): print(*(f"{row*col:3}" for col in range(1, n + 1)))
5

Gnibbler's approach is quite elegant. I went for the approach of constructing a list of list of integers first, using the range function and taking advantage of the step argument.

for n = 12

import pprint
n = 12
m = list(list(range(1*i,(n+1)*i, i)) for i in range(1,n+1))
pprint.pprint(m)
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24], [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36], [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48], [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60], [6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72], [7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84], [8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96], [9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108], [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120], [11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132], [12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]]

Now that we have a list of list of integers that is in the form that we want, we should convert them into strings that are right justified with a width of one larger than the largest integer in the list of lists (the last integer), using the default argument of ' ' for the fillchar.

max_width = len(str(m[-1][-1])) + 1
for i in m: i = [str(j).rjust(max_width) for j in i] print(''.join(i)) 1 2 3 4 5 6 7 8 9 10 11 12 2 4 6 8 10 12 14 16 18 20 22 24 3 6 9 12 15 18 21 24 27 30 33 36 4 8 12 16 20 24 28 32 36 40 44 48 5 10 15 20 25 30 35 40 45 50 55 60 6 12 18 24 30 36 42 48 54 60 66 72 7 14 21 28 35 42 49 56 63 70 77 84 8 16 24 32 40 48 56 64 72 80 88 96 9 18 27 36 45 54 63 72 81 90 99 108 10 20 30 40 50 60 70 80 90 100 110 120 11 22 33 44 55 66 77 88 99 110 121 132 12 24 36 48 60 72 84 96 108 120 132 144

and demonstrate the elasticity of the spacing with a different size, e.g. n = 9

n=9
m = list(list(range(1*i,(n+1)*i, i)) for i in range(1,n+1))
for i in m: i = [str(j).rjust(len(str(m[-1][-1]))+1) for j in i] print(''.join(i)) 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81
for i in range(1, 10) : for j in range(1, 10): print(repr(i*j).rjust(4),end=" ")
print()
print()

Output:

 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81 

or this one

for i in range(1, 11): for j in range(1, 11): print(("{:6d}".format(i * j,)), end='')
print()

the result is :

 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

Or you could just do this (not as simplistic as the others but it works):

def main(): rows = int(input("Enter the number of rows that you would like to create a multiplication table for: ")) counter = 0 multiplicationTable(rows,counter)
def multiplicationTable(rows,counter): size = rows + 1 for i in range (1,size): for nums in range (1,size): value = i*nums print(value,sep=' ',end="\t") counter += 1 if counter%rows == 0: print() else: counter
main()

this one looks pretty neat:

 print '\t\t\t=======================================' print("\t\t\t\tMultiplication Tables") print '\t\t\t=======================================\n' for i in range(1,11): print '\t', i, print print("___________________________________________________________________________________________________________________") for j in range(1,11): print("\n") print j, '|', for k in range(1,11): print '\t', j * k, print("\n")
0

Creating Arithmetic table is much simpler but i thought i should post my answer despite the fact there are so many answers to this question because no one talked about limit of table.

Taking input from user as an integer

num = int(raw_input("Enter your number"))

Set limit of table, to which extent we wish to calculate table for desired number

lim = int(raw_input("Enter limit of table"))

Iterative Calculation starting from index 1

In this, i've make use of slicing with format to adjust whitespace between number i.e., {:2} for two space adjust.

for b in range(1, lim+1): print'{:2} * {:2} = {:2}'.format(a, b, a*b)

Final CODE:

num = int(raw_input("Enter your number"))
lim = int(raw_input("Enter limit of table"))
for b in range(1, lim+1): print'{:2} * {:2} = {:2}'.format(a, b, a*b)

OUTPUT:

Enter your number 2
Enter limit of table 20 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 2 * 10 = 20 2 * 11 = 22 2 * 12 = 24 2 * 13 = 26 2 * 14 = 28 2 * 15 = 30 2 * 16 = 32 2 * 17 = 34 2 * 18 = 36 2 * 19 = 38 2 * 20 = 40
2

For this print as following

 print "%d X %d"%(row, col)

It will print as 2 X 3.

You can accomplish the effect you're looking for much more easily by putting one of the loops inside the print call.

n = int(input('Please enter a positive integer between 1 and 15: '))
for row in range(1, n+1): print('\t'.join(str(row * col) for col in range(1, n+1)))

This creates a generator that yields the string values of row*1, row*2, ... row*n, joins each of those values with a tab character, and passes the resulting string to print().

Your problem is that print adds a newline, and you don't want all those newlines.

One way to do it is to build a string for each line, and then print out the whole line in one print statement.

n=int(input('Please enter a positive integer between 1 and 15: '))
for row in range(1,n+1): s = '' for col in range(1,n+1): s += '{:3} '.format(row * col) print(s)

The magic is in the '{:3} '.format bit. It's tricky, so here's a tutorial:

Here's the code in action:

Please enter a positive integer between 1 and 15: 4 1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16 
n=int(input('Please enter a positive integer between 1 and 15: '))
for row in range(1,n+1): for col in range(1,n+1): print(row*col, "\t",end = "") print()
#the "\t" adds a tab each time, and the end = "" prints your string horizontally.

Here's my take for organizing the output:

for row in range(1, 11): for col in range(1, 11): num = row * col if num < 10: blank = ' ' # 2 blanks else: if num < 100: blank = ' ' # 1 blank print(blank, num, end = '') # Empty string print() # Start a new line
3

This works pretty well for a standard multiplication table that is easy to explain in terms of coding for beginners:

x = 12
y = 12
print ' ',
for fact in range(1, x+1): str(fact).rjust(6),
for fact in range(1, y+1): print print fact, for i in range(1,y+1): product = i * fact print str(product).rjust(5), print
for x in range(1, 11): for y in range(1, 11): z = x * y print(z, end="\t") print() #creates the space after the loop

The code above will produce this result:

There is plenty of answer to the multiplication table. Here I did by calling the function. Feel free to improve.

 num = int(input('Please enter time tables for printing: ')) upperLimit = int(input('Please enter upper limit: ')) def printTable(num, upperLimit): for i in range(0, upperLimit+1): print(num, 'x', i, '=', num * i) def main(): printTable(num, upperLimit) main() 

This works well for me:

num, b, c, count = int(input('Enter a number')), ' ', ' ', 0
for i in range(1, num+1): if i > 9: b = b + str(i) + ' ' c = c + str(i) + '|' else: b = b + str(i) + ' ' c = c + str(i) + ' |' for a in range(1, num+1): d = str(a*i) for letter in d: count += 1 c = c + str(a*i) + ' '*(5-count) count = 0 c = c + '\n '
print((' ')*4 + b)
print((' ')*6 + ('-'+ ' ')*num)
print(c)

It works multiplying up to 99.

Output when entering the number 12:

 1 2 3 4 5 6 7 8 9 10 11 12 - - - - - - - - - - - - 1 |1 2 3 4 5 6 7 8 9 10 11 12 2 |2 4 6 8 10 12 14 16 18 20 22 24 3 |3 6 9 12 15 18 21 24 27 30 33 36 4 |4 8 12 16 20 24 28 32 36 40 44 48 5 |5 10 15 20 25 30 35 40 45 50 55 60 6 |6 12 18 24 30 36 42 48 54 60 66 72 7 |7 14 21 28 35 42 49 56 63 70 77 84 8 |8 16 24 32 40 48 56 64 72 80 88 96 9 |9 18 27 36 45 54 63 72 81 90 99 108 10|10 20 30 40 50 60 70 80 90 100 110 120 11|11 22 33 44 55 66 77 88 99 110 121 132 12|12 24 36 48 60 72 84 96 108 120 132 144 
table = int(input("Enter a Positive Number: "))
for i in range(1,11): print(table, 'X', i, '=', table*i)
print("See you later, Alligator!")
2
for i in range(2,11): print("=============") print("~table of {}~".format(i)) print("=============") for j in range(1,11): print("{} X {} = {}".format(i, j, i*j))
print("=============")
1

Use this code. It works MUCH better than any of the answer here. I had to do this for school, and after putting about 4 hours into this I can tell you that it works FLAWLESSLY.

def returnValue(int1, int2): return int1*int2
startingPoint = input("Hello! Please enter an integer: ")
endingPoint = input("Hello! Please enter a second integer: ")
int1 = int(startingPoint)
int2 = int(endingPoint)
spacing = "\t"
print("\n\n\n")
if int1 == int2: print("Your integers cannot be the same number. Try again. ")
if int1 > int2: print("The second number you entered has to be greater than the first. Try again. ")
for column in range(int1, int2+1, 1): #list through the rows(top to bottom) if column == int1: for y in range(int1-1,int2+1): if y == int1-1: print("", end=" \t") else: individualSpacing = len(str(returnValue(column, y))) print(y, " ", end=" \t") print() print(column, end=spacing) for row in range(int1, int2+1, 1): #list through each row's value. (Go through the columns) #print("second range val: {:}".format(row)) individualMultiple = returnValue(row, column) print(individualMultiple, " ", end = "\t") print("")

Output:Result after entering 5 and 8

Or,Result after entering 40 and 45

Also:

table = 12
for i in range(1,11): print(i*table)
1