Given a number, you have to print the multiplication table of that particular number. Use the format() function to print the multiplication table.
Format is a function that formats that string according to their arguments, to modify data types for the printing purpose.
Format is a function that formats that string according to their arguments, to modify data types for the printing purpose.
Write a Python Program to Print Multiplication Table Using Format Function Inside Print
In the main program file, take a variable number and using the input function call ask a user to integer number, then once the user enters the number it'll be asking format so convert the string to an integer using int() function call type conversion.
number = int(input("Enter a Number : "))
Once the user enters a number used for a loop to iterate between the number 1 to 11, get the range between 1 to 11 and get the index variable.
Using the index variable multiply it with the number. Use the print function and format function at the same time to format the output of that multiplication table. use the argument 0 as a number, argument 1 as index, and a number multiplied by index. print the multiplication table as shown below.
for index in range(1, 11):
print("{0} x {1} = {2}".format(number, index, number * index))
Python Code
===================
number = int(input("Enter a Number : "))
for index in range(1, 11):
print("{0} x {1} = {2}".format(number, index, number * index))
for index in range(1, 11):
print("{0} x {1} = {2}".format(number, index, number * index))
OUTPUT
===============
Enter a Number : 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
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
OUPUT 2
=======================
Enter a Number : 22
22 x 1 = 22
22 x 2 = 44
22 x 3 = 66
22 x 4 = 88
22 x 5 = 110
22 x 6 = 132
22 x 7 = 154
22 x 8 = 176
22 x 9 = 198
22 x 10 = 220
22 x 1 = 22
22 x 2 = 44
22 x 3 = 66
22 x 4 = 88
22 x 5 = 110
22 x 6 = 132
22 x 7 = 154
22 x 8 = 176
22 x 9 = 198
22 x 10 = 220
Conclusion
=====================
Try changing the range of the for loop and modify it to 21 and see the difference. To print more tables from 1 to 20 range.
0 Comments