How to Print a Table in Python

If you’re looking to print a table in Python, you’ve come to the right place. In this blog post, we’ll show you how to print a table in Python using the built-in library, tabulate.

Checkout this video:

Introduction

Printing a table in Python is a relatively simple task. However, there are a few different ways to go about it, and the method you use will depend on your particular needs. In this article, we’ll show you how to print a table in Python using both the built-in print() function and the third-party tabulate module.

The Print Function

In Python, the print function is used to print to the standard output stream. This function takes one or more values and prints them to the standard output stream, separating each value by a space. Optionally, the values can be printed with a certain kind of formatting.
The print function is not the only way to output data in Python. Another common method is to use the write function, which writes data to a file or other output stream.

When you use the print function, it is important to understand how it works so that you can format your data in a way that is most readable for your users. In this article, we will take a look at how the print function works and how you can use it to format your data.

The Format Function

Python’s built-in format() function is a great way to print tables. The format() function takes two arguments:

the table to print, which must be a list of lists (or another iterable), and
the number of columns in the table.

The following code prints a table with three columns:

table = [
[‘Name’, ‘Age’, ‘Favorite Color’],
[‘Bob’, 19, ‘Blue’],
[‘Carol’, 22, ‘Green’],
[‘Ted’, 23, ‘Purple’]
]

print(format(table, 3))

This code produces the following output:

Name Age Favorite Color
Bob 19 Blue
Carol 22 Green
Ted 23 Purple

The Str Function

The str function is used to create a string representation of an object. The str function is used to create a string representation of an object. In this tutorial, we will see how to use the str function to print a table in Python.

To print a table in Python, we first need to import the tabulate module. We can do this by running the following command:
import tabulate

Once we have imported the tabulate module, we can use the str function to print a table. The str function takes two arguments: the object to be converted into a string, and the width of the column in which the object will be printed. For example, if we wanted to print a table with three columns, we would use the following code:
print(tabulate.str(table, 3))

The output of this code would be:
object1 object2 object3
4 8 12
5 10 15
6 12 18

The repr Function

The repr function is used to generate a string representation of an object. This can be useful for debugging purposes, or for creating output that can be easily parsed by other programs.

To print a table in Python, we can use the repr function to generate a string representation of our data. We can then print this string representation to the console.

Here is an example of how to use the repr function to print a table:

data = [[“Name”, “Age”, “Gender”], [“John”, “20”, “Male”], [“Jane”, “21”, “Female”]] print(repr(data))

This will output the following:

[[‘Name’, ‘Age’, ‘Gender’], [‘John’, ’20’, ‘Male’], [‘Jane’, ’21’, ‘Female’]]

The ascii Function

The ascii() function returns a string containing a printable representation of an object. The given object is converted to its string representation, and the resulting string is returned.

If the object is not a string, it is first converted to a string using the str() function.

The unicode Function

The unicode function is used to print a table in Python. This function takes two arguments: the data to be printed, and the number of columns in the table. The data argument can be a list, tuple, or dictionary. The number of columns is optional; if it is not specified, the unicode function will print the data in one column.

To print a table with three columns, you would use the following code:

unicode(data, 3)

This code would print the following table:

1 2 3
———
a | b | c
d | e | f
g | h | i

Custom Printing Functions

A custom printing function can format and print a table in any desired shape or size.

You can use the pprint() module to customize the way your tables are printed. The pprint() module provides a better way to print dictionaries and lists in Python.

To format and print a table in Python, you can use the following code:

“`py
import pprint

pprint.pprint(table, width=40, compact=True)
“`

Conclusion

In conclusion, you have learned how to print a table in Python using the tabulate library. You have also learned how to format your output and use different printing options.

Scroll to Top