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.
![Code for colored terminal Code for colored terminal](https://www.maixuanviet.com/wp-content/uploads/2021/08/colored_terminal.png)
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:
Python String istitle()
Python Set intersection_update()
Python Program to Count the Number of Occurrence of a Character in String
Python String isnumeric()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python String title()
Python locals()
Python String rpartition()
Python tuple()
Python Program to Represent enum
Python Set issubset()
Python format()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python String join()
Python Program to Calculate the Area of a Triangle
Python String rfind()
Python Program to Shuffle Deck of Cards
Python List reverse()
Python Program to Check the File Size
Python Program to Find Numbers Divisible by Another Number
Python Object Oriented Programming
Python Program to Check If a String Is a Number (Float)
Python Program to Remove Punctuations From a String
Python Set issuperset()
Python Recursion
Python input()
Python String isprintable()
Python Set add()
Python Set symmetric_difference_update()
Python String splitlines()
Python Program to Print Output Without a Newline
Python Tuple index()