Python

Print Colored Text in Python

The colored text output of the script in the terminal makes it more attractive and understandable for the users. The colored text output can be generated in different ways by using the ANSI escape sequences or a particular Python module. The uses of three Python modules and the ANSI escape sequences to print colored text in the terminal by using Python script have been shown in this tutorial.

Using the ANSI Escape Sequence

The colored text can be printed in the terminal without any Python module by using the ANSI Escape Sequence. Some sequences or codes are used here to change the behavior of the terminal. 16 colors or 256 colors can be used for coloring the text of the terminal.

8 foreground and 8 background colors are used in the 16 colors option. The color codes of foreground and background are mentioned below.

Foreground Background
Black 30 Black 40
Red 31 Red 41
Green 32 Green 42
Yellow 33 Yellow 43
Blue 34 Blue 44
Purple 35 Purple 45
Cyan 36 Cyan 46
White 37 White 47

Mostly used color option is the 256 colors. 0 to 255 color codes can be used to generate foreground and background colors by using the 256 colors option. 38 is used to set the foreground and 48 is used to set the background color. The uses of 16 and 256 color codes have been shown in the next two examples.

Example-1: Generate Color Using 16 Color Codes

Create a Python file with the following script that will color a text value by using the 16 color codes at the beginning of the text and both sides of the text. Another text is printed without any color code.

#Set the text value

Text = 'Welcome to LinuxHint'

#Print the colored text with the starting color codes

print('\033[1;34;45m' + Text)

#Print a simple text

print('Blog Site')

#Print the colored text with the starting and ending color codes

print('\033[2;31;43m' + Text + '\033[0;0m')

Output:

The following output will appear after executing the above script. The output shows that when the color code is used at the starting of the text, then the color code will apply for the next text also. When the color code is used on both side of the text, then the effect of the color code that is used on the right side of the code will apply on the next text.

Example-2: Generate Color Using 256 Color Codes

Create a Python file with the following script that will color two text values by using a background color and different foreground colors for two text values.

#Set the first string

Text1 = "Hello"

#Set the second string

Text2 = " World"

#Print both strings with particular background and foreground colors

print("\033[48;5;225m\033[38;5;245m" + Text1 + "\033[38;5;208m" + Text2 + "\033[0;0m")

Output:

The following output will appear after executing the above script.

Using Colored Module

The colored module is one of the modules that is used to color the terminal text with style. You have to install the colored module to use it in the Python script. The detailed information of this module is available in the following URL location.

https://pypi.org/project/colored/

Run the following command to install the colored module.

$ pip3 install colored

The available attributes of this module that can be applied to the output text with the color are mentioned below.

Attribute Codes Purpose
0 reset text.
1 bold text.
2 dim text.
4 underlined text.
5 blink text.
7 reverse text.
8 hidden text.
21 res_bold text.
22 res_dim text.
24 res_underlined text.
25 res_blink text.
27 res_reverse text.
28 res_hidden text.

Example-3: Generate Color Using the Colored Module

Create a Python file with the following script that will print the text with the background color in the first output and print the text with the foreground color, background color, and attribute in the second output.

#Import required modules

from colored import fg, bg, attr

#Set the text value

text = "Python Programming"

#Print the text with the foreground color

print("%s %s %s" %(fg(50), text, attr(0)))

#Print the text with foreground and background colors and reverse attribute

print("%s %s %s %s" %(fg(25), bg(170), text, attr(7)))

Output:

The following output will appear after executing the above script.

Using the Termcolor Module

The termcolor module is another module of Python that is used to color the terminal text with the style. You have to install the termcolor module to use it in the Python script. The detailed information of this module is available in the following URL location.

https://pypi.org/project/termcolor/

Run the following command to install the termcolor module.

$ pip3 install termcolor

Example-4: Generate Color Using Termcolor Module

Create a Python file with the following script that will print the text with the foreground color in the first output and print the text with the foreground color, background color, and attributes in the second and third output.

#Import colored module from termcolor

from termcolor import colored

#Set the string values

text1 = "Learn"

text2 = "Python"

#Print string with foreground color

print(colored(text1,'magenta'))

#Print string with foreground color with attribute

print(colored(text2,'yellow', attrs=['reverse', 'bold']))

#Print string with foreground and background colors

print(colored('Learn Python Programming','red','on_cyan'))

Output:

The following output will appear after executing the above script.

Using the Colorama Module

The colorama module is another module of Python that is used to color the terminal text with the style. You have to install the colorama module to use it in the Python script. The detailed information on this module is available in the following URL location.

https://pypi.org/project/colorama/

Run the following command to install the colorama module.

$ pip3 install colorama

Example-5: Generate Color Using Colorama Module

Create a Python file with the following script that will print the text with the background color in the first output, print the text with the foreground and background color in the second output, and reset all previous styles before printing the third output.

#Import necessary modules

from colorama import Back, Fore, Style

#Print text with background color

print(Back.BLUE + 'First Text')

#Print text with background and foreground colors

print(Back.GREEN + Fore.RED + 'Second Text')

#Print text after resetting all previous  style

print(Style.RESET_ALL, 'Normal text')

Output:

The following output will appear after executing the above script.

Conclusion

Different ways to print the colored text in the terminal have been shown in this tutorial by using multiple examples. Python users can use any of the Python modules shown in this tutorial or the ANSI escape sequences to print the colored text in the terminal.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.