This article demonstrates how to print formatted text in Linux terminal using Python programming language.
Formatted text(also called styled text or rich text) as opposed to plain text, has styling information like:
- color (text color, background color),
- style (bold or italic),
- and some other special features like Strike-through text, underlined text, hyperlinks, etc.
In a Linux Terminal, ANSI escape codes (or escape sequences) are used to control the formatting, color, and other output options.
To encode this formatting information, certain sequences of bytes are embedded into the text, which the terminal looks for and interprets as commands and executes them.
Let us go through a basic example of printing formatted text through a simple Python print statement!
print ( '\x1b[3;31;43m' + 'Hello world!' + '\x1b[0m' )
|
If you run this code on Linux Terminal, you will obtain an output like this:

As you can see, the output has:
- text-color(foreground color): Red
- background color: Yellow
- text-style: Italic
Now, let us try to understand the meaning of the ANSI escape codes used in above print statement.
Now, we create a class in Python to achieve required formatting systematically!
class TextFormatter:
COLORCODE = {
'k' : 0 ,
'r' : 1 ,
'g' : 2 ,
'y' : 3 ,
'b' : 4 ,
'm' : 5 ,
'c' : 6 ,
'w' : 7
}
FORMATCODE = {
'b' : 1 ,
'f' : 2 ,
'i' : 3 ,
'u' : 4 ,
'x' : 5 ,
'y' : 6 ,
'r' : 7 ,
'h' : 8 ,
's' : 9 ,
}
def __init__( self ):
self .reset()
def reset( self ):
self .prop = { 'st' : None , 'fg' : None , 'bg' : None }
return self
def cfg( self , fg, bg = None , st = None ):
return self .reset().st(st).fg(fg).bg(bg)
def st( self , st):
if st in self .FORMATCODE.keys():
self .prop[ 'st' ] = self .FORMATCODE[st]
return self
def fg( self , fg):
if fg in self .COLORCODE.keys():
self .prop[ 'fg' ] = 30 + self .COLORCODE[fg]
return self
def bg( self ,bg):
if bg in self .COLORCODE.keys():
self .prop[ 'bg' ] = 40 + self .COLORCODE[bg]
return self
def format ( self , string):
w = [ self .prop[ 'st' ], self .prop[ 'fg' ], self .prop[ 'bg' ]]
w = [ str (x) for x in w if x is not None ]
return '\x1b[%sm%s\x1b[0m' % ( ';' .join(w), string) if w else string
def out( self , string):
print ( self . format (string))
|
This is the class definition of our text formatter.
In order to use it, save above Python script as TextFormatter.py and open terminal in the same folder where this file is stored.
Then, run Python interpreter through the terminal and import TextFormatter.
Given below is a screen-shot of a sample run:

Here,
- We import TextFormatter class from TextFormatter module using:
from TextFormatter import TextFormatter
- Then, create an object of TextFormatter class using:
cprint = TextFormatter()
- Configure formatting of text using cprint.cfg method. Here, argument 1 is text-color, argument 2 is background color and argument 3 is text style.
cprint.cfg('y', 'g', 'b')
- Now, simply print any statement using cprint.out method like this:
cprint.out("Hello, world!")
So, now, you can print styled text on terminal with ease!
This blog is contributed by Nikhil Kumar.