JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Printing data in a table in Python

Author:JIYIK Last Updated:2025/05/07 Views:

Lists can store multiple elements in a specific order. However, when we print a list, it may be a little unclear whether the data is in row format or not. The data in a list can also be printed in a tabular format. This way, the data will remain neat and organized when you view it, as everything is beautifully arranged in rows and columns.

This tutorial shows you how to print data from a list collection in a tabular format.


format()Printing data in a tabular format using the function in Python

Python enables us to format()perform efficient string formatting using the String function. It gives us the freedom to ensure that we get the output in the desired format.

To display the data in a tabular format, we effectively specify the spaces between the columns and print the data in the list in the same format.

For example,

d = [["Mark", 12, 95], ["Jay", 11, 88], ["Jack", 14, 90]]

print("{:<8} {:<15} {:<10}".format("Name", "Age", "Percent"))

for v in d:
    name, age, perc = v
    print("{:<8} {:<15} {:<10}".format(name, age, perc))

Output:

Name     Age             Percent   
Mark     12              95        
Jay      11              88        
Jack     14              90 

tabulatePrinting data in a tabular format using the module in Python

tabulateThe module has methods that can be used to print data in a simple and elegant table structure.

We just need to specify the data and column names to the function from this module tabulate()and it will do the rest.

For example,

from tabulate import tabulate

d = [["Mark", 12, 95], ["Jay", 11, 88], ["Jack", 14, 90]]

print(tabulate(d, headers=["Name", "Age", "Percent"]))

Output:

Name      Age    Percent
------  -----  ---------
Mark       12         95
Jay        11         88
Jack       14         90

Note that there are other modules in Python that can print data in different tabular styles. Some of them are PrettyTable, termtable, texttableetc.


pandas.DataFrame()Use the function to print data in a tabular format in Python

pandasThe library allows us to create DataFrames in Python. These DataFrames are generally used to store datasets and enable efficient processing of the data stored in them. We can also perform various types of operations on DataFrames.

We can very easily create a DataFrame using the data in the list and print it as shown below.

import pandas as pd

d = [["Mark", 12, 95], ["Jay", 11, 88], ["Jack", 14, 90]]

df = pd.DataFrame(d, columns=["Name", "Age", "Percent"])
print(df)

Output:

   Name  Age  Percent
0  Mark   12       95
1   Jay   11       88
2  Jack   14       90

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Implementing a Low-Pass Filter in Python

Publish Date:2025/05/07 Views:89 Category:Python

Low pass filter is a term in signal processing basics and is often used to filter signals to obtain more accurate results. This tutorial will discuss the low-pass filter and how to create and implement it in Python. A low-pass filter is use

Implementing Curl command in Python using requests module

Publish Date:2025/05/07 Views:97 Category:Python

requests This article will discuss and implement different curl commands using the module in Python . requests Installing modules in Python Python provides us with requests the module to execute curl command. Install it in Python 3 using Pi

Using fetchall() in Python to extract elements from a database

Publish Date:2025/05/07 Views:171 Category:Python

This article aims to describe fetchall() the working methods of extracting elements from a database using and how to display them correctly. This article will also discuss list(cursor) how functions can be used in programs. fetchall() Extra

Parsing log files in Python

Publish Date:2025/05/07 Views:106 Category:Python

Log files contain information about events that occurred during the operation of a software system or application. These events include errors, requests made by users, bugs, etc. Developers can further scan these usage details to find poten

Declaring a variable without a value in Python

Publish Date:2025/05/07 Views:57 Category:Python

A variable is a reserved memory location that can store some value. In other words, variables in a Python program provide data to the computer to process operations. Every value in Python has a data type. There are numbers, lists, tuples, e

Defining class global variables in Python

Publish Date:2025/05/07 Views:81 Category:Python

A global variable is a variable that is visible and available in every part of the program. Global variables are also not defined in any function or method. On the other hand, local variables are defined in functions and can be used only in

Incrementing loop step by 2 in Python

Publish Date:2025/05/07 Views:199 Category:Python

In each iteration, for the loop increases the counter variable by a constant. A loop with the sequence 0, 2, 4, 6 for will increase the counter variable by 2 each iteration. This article will show you some for ways to increment by 2 in a lo

Pool map with multiple parameters in Python

Publish Date:2025/05/07 Views:104 Category:Python

multiprocessing This article will explain different ways to perform parallel function execution using the module in Python . multiprocessing The module provides functionality to perform parallel function execution using multiple inputs and

Python if...else in Lambda function

Publish Date:2025/05/07 Views:68 Category:Python

lambda Functions are used to implement some simple logic in Python and can be thought of as anonymous functions. It can have multiple parameters but only one expression, just def like any other function defined using the keyword. We can def

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial