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
.
\x1b
calls a function. You can also use\033
for the same purpose.38;2;r;g;b
helps to set RGB color.5;86;243
are the rgb color for blue (the color of the logo of Programiz).m
is the function name. Here,m
means 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:
How to get current date and time in Python?
Python Program to Get the Last Element of the List
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Input, Output and Import
Python String islower()
Python Program to Transpose a Matrix
Python Set isdisjoint()
Python Deep Learning Cookbook - Indra den Bakker
Python Program to Copy a File
Python memoryview()
Python Program to Get the File Name From the File Path
Python strptime()
Python all()
Python Tuple
Python String maketrans()
Python Dictionary get()
Python Set difference_update()
Python File I/O Operation
Python String center()
Python Operator Overloading
Python filter()
Python set()
Python Program to Capitalize the First Character of a String
Python slice()
Python vars()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python property()
Python round()
Python Program to Sort a Dictionary by Value
Python Program to Display Calendar
Python setattr()
Python bool()