Table of Contents
In this example, you will learn to print colored text to the terminal.
To understand this example, you should have the knowledge of the following Python programming topics:
1. Example 1: Using ANSI escape sequences
print('\x1b[38;2;5;86;243m' + 'Programiz' + '\x1b[0m')
Output
Programiz
The working of the above line of code is shown in the figure below.

Let’s understand the escape code \x1b[38;2;5;86;243m.
\x1bcalls a function. You can also use\033for the same purpose.38;2;r;g;bhelps to set RGB color.5;86;243are the rgb color for blue (the color of the logo of Programiz).mis the function name. Here,mmeans SGR (Select Graphics Rendition) function.
For more information regarding the ANSI escape code, you can refer to ANSI escape code.
2. Example 2: Using python module termcolor
from termcolor import colored
print(colored('Programiz', 'blue'))
Output
Programiz
Using the module termcolor, you can get the desired output. Also, you can set different styles of the text using this module.
The first parameter of colored() is the text and the second parameter is the color.
Related posts:
Deep Learning with Python - Francois Chollet
APIs in Node.js vs Python - A Comparison
Python Program to Find the Factors of a Number
Python min()
Python type()
Python Program to Check the File Size
Python Program to Count the Number of Digits Present In a Number
Python Program to Represent enum
Python Program to Convert Decimal to Binary Using Recursion
Python Program to Get the Full Path of the Current Working Directory
Python String expandtabs()
Python open()
Python Program to Return Multiple Values From a Function
Python Program to Compute all the Permutation of the String
Python sleep()
Python hasattr()
Python Global Keyword
Python Program to Find the Sum of Natural Numbers
Python Operators
Python callable()
Python Program to Count the Occurrence of an Item in a List
Python Tuple
Python Namespace and Scope
Python isinstance()
Python List remove()
Python String splitlines()
Python issubclass()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python List insert()
Python Set intersection_update()
Python Program to Make a Flattened List from Nested List
Python String isdecimal()