TextTable module in Python Last Updated : 11 Oct, 2020 Comments Improve Suggest changes 8 Likes Like Report It is a python module, which helps us to print table on terminal. It is one of the basic python modules for reading and writing text tables in ASCII code. It aims to make the interface as similar as possible like csv module in Python. The texttable module supports both fixed-size tables (where column sizes are pre-determined) and dynamic-size tables (where columns can added or removed). Installation: pip install texttable Step-by-step Approach: Import required module. Python3 # Import required module import texttable Create an object of texttable() Python3 # Creating object tableObj = texttable.Texttable(self,max width) # max_width must be an integer,whose value is maximum width of the table # if set to 0, size is unlimited (self adjustible according to text inside cell), # therefore cells won't be wrapped so it's recommended to use 0 Use set_cols_align() method to create columns. Python3 # Creating columns tableObj.set_cols_align(["l", "l", "r", "c"]) # Set the desired columns alignment: # "l" refers to column flushed left # "c" refers to column centered # "r" refers to column flushed right Use set_cols_dtype() to set datatype of each column. However, this step is optional. Python3 # Set datatype tableObj.set_cols_dtype(["t", "i", "f", "a"]) # texttable objects supports five types of data types: # "t" refers to text # "f" refers to decimal # "e" refers to exponent # "i" refers to integer # "a" refers to automatic Use set_cols_valign() to adjust columns. Python3 # Adjust Columns tableObj.set_cols_valign(["t", "t", "m", "b"]) # Set the desired columns vertical alignment the elements of the # array should be either "t", "m" or "b": # "t" refers to column aligned on the top of the cell # "m" refers to column aligned on the middle of the cell # "b" refers to column aligned on the bottom of the cell Use add_rows() method to insert rows into the table Python3 # Adding rows table.add_rows([ ["Text_Heading", "Int_Heading", "Float_Heading", "Auto_Heading"], ["Data1", 9, 1.23456789, "GFG"], ["Data2", 1, 9.87654321, "g4g"], ]) # add_rows(self, rows, header=True): # The 'rows' argument can be either an iterator returning arrays, or a # by-dimensional array. # 'header' specifies if the first row should be used as the header of the table Use draw() method to display the table. Python3 print(tableObj.draw()) The table illustration would be something like this: Below is a program based on the above approach: Python3 # Import required module import texttable # Create texttable object tableObj = texttable.Texttable() # Set columns tableObj.set_cols_align(["l", "r", "c"]) # Set datatype of each column tableObj.set_cols_dtype(["a", "i", "t"]) # Adjust columns tableObj.set_cols_valign(["t", "m", "b"]) # Insert rows tableObj.add_rows([ ["ORGANIZATION", "ESTABLISHED", "CEO"], ["Google", 1998, "Sundar Pichai"], ["Microsoft", 1975, "Satya Nadella"], ["Nokia", 1865, "Rajeev Suri"], ["Geeks for Geeks", 2008, "Sandeep Jain"], ["HackerRank", 2007, "Vivek Ravisankar"] ]) # Display table print(tableObj.draw()) Output: Create Quiz Comment A akshaypawar4 Follow 8 Improve A akshaypawar4 Follow 8 Improve Article Tags : Python python-modules Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like