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:
Python Program to Reverse a Number
Deep Learning in Python - LazyProgrammer
Python String endswith()
Python Program to Check If Two Strings are Anagram
Python List index()
Python str()
Python Set intersection()
Python String islower()
Python Set pop()
Python Program to Differentiate Between del, remove, and pop on a List
Python isinstance()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Deep Learning with Python - Francois Cholletf
Python Function Arguments
Python complex()
Python hash()
Python bytearray()
Python Dictionary keys()
Python super()
Python Program to Access Index of a List Using for Loop
Machine Learning with Python for everyone - Mark E.Fenner
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Set symmetric_difference()
Python help()
Python filter()
Python Program to Find the Largest Among Three Numbers
Python Package
Python Program to Measure the Elapsed Time in Python
Python Input, Output and Import
Python Program to Check if a Number is Positive, Negative or 0
Python List reverse()
Python String maketrans()