PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
  • Quizzes
  • Code Editor
Home » Python » Programs and Examples » Create Multiplication Table in Python

Create Multiplication Table in Python

Updated on: March 27, 2025 | Leave a Comment

Printing a multiplication table is a common task when learning basics of Python. It helps solidify the concepts of loops, nested loops, string formatting, list comprehension, and functions in Python programming.

A multiplication table is a list of multiples of a number and is also commonly referred to as the times table. We can obtain the times tables of a number by multiplying the given number with whole numbers.

In this tutorial, we will explore various ways to create and display multiplication tables using Python.

Table of contents

  • Print the multiplication table for a specific number using for loop
  • Print Full Multiplication Table Using Nested Loops
  • Create multiplication table using while loop
  • Create multiplication table using List Comprehension
  • Using Functions
  • Using Libraries (e.g., Pandas)
  • Using String Formatting for Better Alignment
  • Interactive Multiplication Table Generator
  • Conclusion

Print the multiplication table for a specific number using for loop

Here’s the steps and Python code to print a multiplication table for a specified number:

This method is straightforward and uses a single for loop to print the multiplication table for a specific number. By iterating through a range of values, it calculates the product of the given number with each value in the range and displays the result in a formatted manner. Below are the steps.

Steps:

  1. Get Input: Prompt the user to enter the number for which they want the multiplication table. Store the input number in a variable (e.g., number).
  2. Set Range: Determine the range of multipliers for the table. Typically, multiplication tables go from 1 to 10 or 1 to 12. Create a loop that iterates through this range (e.g., using range(1, 11) for 1 to 10).
  3. Calculate and Print: Inside the loop, for each multiplier multiply the input number by the current multiplier.
  4. Print the multiplication expression and the result in a formatted way (e.g., “number x multiplier = result”).

Let’s see how to take a number as input and print its multiplication table up to 10 in Python.

Code Example

# Multiplication table for a specific number
number = int(input("Enter a number: "))
for i in range(1, 11):
    print(f"{number} x {i} = {number * i}")Code language: Python (python)

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

Explanation

  • The input() function is used to take input from the user. The int() function converts the input (which is taken as a string by default) into an integer.
  • The for loop iterates through numbers 1 to 10, where each value represents the multiplier for the given number.
  • At each iteration, the loop picks the current value from the range and calculates the product by multiplying it with the input number. This calculated result is then displayed in a formatted string that clearly shows the operation and the result (e.g., “5 x 3 = 15”).
  • Each iteration calculates the product of the number and the current value of i.
  • The result is formatted and printed.

Print Full Multiplication Table Using Nested Loops

The multiplication table from 1 to 10 is a table that shows the products of numbers from 1 to 10. This method generates a complete multiplication table for numbers 1 through 10.

In this approach nested loop is used, where one loop is placed inside another. The outer loop determines the rows of the table, and the inner loop iterates through each column to calculate and display the product of the numbers.

Code Example

# Full multiplication table from 1 to 10
for i in range(1, 11):
  print('multiplication table of:', i)
  for j in range(1, 11):
    print(f" {i * j}", end="")
  print() # Move to the next line after each rowCode language: Python (python)

Output:

multiplication table of: 1
1 2 3 4 5 6 7 8 9 10
multiplication table of: 2
2 4 6 8 10 12 14 16 18 20
multiplication table of: 3
3 6 9 12 15 18 21 24 27 30
...
multiplication table of: 10
10 20 30 40 50 60 70 80 90 100

Explanation

  • The outer loop iterates through the rows (numbers 1 to 10).
  • The inner loop iterates through the columns.
  • Each product is printed on the same row using end="" to add a space.
  • print() is used to move to the next line after each row.

Create multiplication table using while loop

Below is the example to create multiplication table from 1 to 10 using a while loop in Python.

Code Example

# Initialize the outer loop counter
i = 1

# Outer loop for each multiplication table
while i <= 10:
    print(f"Multiplication table of: {i}")  # Print header for the table
    
    # Initialize the inner loop counter
    j = 1
    
    # Inner loop for each multiplier
    while j <= 10:
        print(f" {i * j}", end="")  # Print the product on the same line
        j += 1  # Increment the inner loop counter
    
    print()  # Move to the next line after each row
    i += 1  # Increment the outer loop counterCode language: Python (python)

Output:

Multiplication table of: 1
1 2 3 4 5 6 7 8 9 10
Multiplication table of: 2
2 4 6 8 10 12 14 16 18 20
Multiplication table of: 3
3 6 9 12 15 18 21 24 27 30
...
Multiplication table of: 10
10 20 30 40 50 60 70 80 90 100

Explanation

  1. An outer while loop is used to iterate through each number i from 1 to 10.
  2. Inside the outer loop, another while loop iterates through numbers j from 1 to 10.
  3. The product of i and j is calculated and printed, and the end="" parameter ensures the numbers are printed on the same line.
  4. The print() statement after the inner loop ensures each multiplication table starts on a new line.
  5. Both i and j are incremented within their respective loops to avoid infinite loops.

Create multiplication table using List Comprehension

This approach uses a compact syntax to create and print the multiplication table. List comprehension is a concise way to generate lists in Python. It allows for looping and conditional logic to be written in a single line, making the code more readable and compact.

In this case, it is used to create a 2D list where each inner list represents a row of the multiplication table.

Code Example

# Full multiplication table using list comprehension
multiplication_table = [[i * j for j in range(1, 11)] for i in range(1, 11)]

for row in multiplication_table:
    print(' '.join(map(str, row)))Code language: Python (python)

Explanation

  • A nested list comprehension generates the table as a 2D list.
  • Each inner list represents a row.
  • The print() function, combined with join(), formats each row as a string.

Using Functions

Encapsulating the logic in a function improves reusability and readability. By defining a Python function, you can separate the logic of printing a multiplication table from the rest of the program, making the code more modular and easier to maintain. Functions also allow for repeated use with different inputs, which eliminates redundancy and simplifies debugging and updates in larger programs.

Let’s define a function print_multiplication_table which takes a number as an argument and prints its multiplication table. The logic inside the function is reusable.

Code Example

def print_multiplication_table(number):
    for i in range(1, 11):
        print(f"{number} x {i} = {number * i}")

# Call the function
num = int(input("Enter a number: "))
print('Multiplication table of', num)
print_multiplication_table(num)Code language: Python (python)

Output:

Enter a number:  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

Using Libraries (e.g., Pandas)

For more advanced formatting and visualization, you can use libraries like pandas.

Code Example

import pandas as pd

# Create a DataFrame for the multiplication table
def create_multiplication_table():
    data = {i: [i * j for j in range(1, 11)] for i in range(1, 11)}
    df = pd.DataFrame(data, index=range(1, 11))
    return df

# Generate and display the table
multiplication_table_df = create_multiplication_table()
print(multiplication_table_df)Code language: Python (python)

Output:

   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
...
10 10 20 30 40 50 60 70 80 90 100

Explanation

  • A dictionary comprehension creates the data for the table.
  • pandas.DataFrame formats it into a tabular structure.
  • The resulting DataFrame is printed neatly.

Using String Formatting for Better Alignment

String formatting in Python is a technique used to structure and align text or numbers in a desired format. It allows you to control the appearance of output, such as specifying field widths, alignment, and precision.

This method neatly aligns the output in columns so the multiplication table becomes more readable and easily understood.

Code Example

print('Full multiplication table with alignment')
for i in range(1, 11):
    for j in range(1, 11):
        print(f"{i * j:4}", end="")
    print()Code language: Python (python)

In the given example, f"{i * j:4}" ensures that each value in the multiplication table occupies 4 character spaces, resulting in a neatly aligned output.

Output:

Full multiplication table with alignment
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

Interactive Multiplication Table Generator

This approach allows the user to specify the range for rows and columns, making it a highly dynamic and interactive solution. This method also works well in scenarios where the table size needs to vary based on user input.

Code Example

# Interactive multiplication table
def interactive_table():
    rows = int(input("Enter the number of rows: "))
    cols = int(input("Enter the number of columns: "))

    for i in range(1, rows + 1):
        print('multiplication table of:', i)
        for j in range(1, cols + 1):
            print(f"{i * j}", end=" ")
        print()

interactive_table()Code language: Python (python)

Output:

Enter the number of rows:  5
Enter the number of columns: 10

multiplication table of: 1
1 2 3 4 5 6 7 8 9 10
multiplication table of: 2
2 4 6 8 10 12 14 16 18 20
multiplication table of: 3
3 6 9 12 15 18 21 24 27 30
multiplication table of: 4
4 8 12 16 20 24 28 32 36 40
multiplication table of: 5
5 10 15 20 25 30 35 40 45 50

Explanation:

  • The programs takes the number of rows and columns using input() function.
  • Nested loops generate the table accordingly.

Conclusion

In this tutorial, we explored multiple ways to print a multiplication table in Python, ranging from simple loops to advanced formatting with libraries. Each method has its unique advantages and use cases:

  • Using a Simple Loop: Best for beginners to learn basic loops and multiplication. Ideal for generating a table for a single number.
  • Using Nested Loops: Great for creating complete multiplication tables for a range of numbers. Useful for understanding how loops interact with each other.
  • Using List Comprehension: A compact and Pythonic approach for generating tables in a structured format. Perfect for those looking to write clean and efficient code.
  • Using Functions: Encourages modularity and reusability. Suitable for projects where the multiplication logic might be reused multiple times.
  • Using Libraries (e.g., Pandas): Ideal for advanced users who need to visualize or manipulate multiplication tables in a tabular format. Perfect for data analysis and presentation.
  • Using String Formatting: Ensures well-aligned and professional-looking tables. Useful for applications requiring clear and consistent output.
  • Interactive Multiplication Table Generator: Provides flexibility for users to define custom ranges. Suitable for interactive programs and educational tools.

Choose the method that best suits your needs based on your skill level and the problem you are trying to solve. Happy coding!

Filed Under: Programs and Examples, Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Image

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Programs and Examples Python Python Basics

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Programs and Examples Python Python Basics
TweetF  sharein  shareP  Pin

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2025 pynative.com

Advertisement