Python

How to Use Colorama in Python

Many built-in modules and libraries exist in Python to print the colored text in the terminal. The Colorama is one of the built-in Python modules to display the text in different colors. It is used to make the code more readable. Three formatting options are available in this module for coloring text. These are Back, Fore and Style. The background or foreground color of the text and the style of the text can be changed by this module. Different uses of this module have been explained in this tutorial.

Python Colorama init() Function

The init() function is used to initialize the Colorama before using it in the script. It can be used without any argument or with an argument. Some keyword arguments can be used in the init() function which is described below.

Argument Name Description
Autoreset It is used to reset the color and style after each line when the value of this argument is set to True.
Strip It is used to remove the ANSI code from the output when the value of this argument is set to True.
Convert It is used to convert the ANSI code of the output when the value of this argument is set to True.
Wrap It is used to disable the overriding task when the value of this argument is set to False.

Terminal Colors

The following colors can be used by the Colorama as the background and font color of the terminal.

  1. RED
  2. GREEN
  3. BLUE
  4. WHITE
  5. YELLOW
  6. MAGENTA
  7. CYAN
  8. WHITE

Style Settings

Three text styles are available in Colorama to change the thickness of the text. These are:

  1. DIM
  2. NORMAL
  3. BRIGHT

Different ways to set the background and font colors for the terminal text have been in the next part of this tutorial.

Example 1: Print Text with Background and Font Color

Create a Python file with the following script that shows the way to change the background and the font color using the Colorama module. The autoreset argument has been used in the init() function to reset the previous color and style after each line. At first, the background color is set to RED, and the font color is set to BLUE for the text, “Welcome to LinuxHint”. Next, the background color is set to GREEN for the text, ‘I like programming’.

#Import required modules
import colorama
from colorama import Fore, Back, Style

#Initialize colorama
colorama.init(autoreset=True)

#Print text using background and font colors
print(Back.RED + Fore.BLUE + "Welcome to LinuxHint")
#Add newline
print()
#Print text using background color
print(Back.GREEN + "I like programming")

Output
The following output will appear after executing the above script. The output shows that the color setting for the first text data didn’t overwrite the color setting of the second text and the color setting has been applied separately for each text because autoreset argument is set True in the script.

Example 2: Print Text with Color and Style

Create a Python file with the following script to know the way of setting the style for the terminal text with the color. At first, the font color is set to CYAN for the text, ‘Welcome to Linuxhint’. Next, the background color is set to YELLOW, and the style is set to DIM for the text, ‘Learn Python. The Style.RESET_ALL property will reset all previous color and style settings. Next, the font color is set to RED and the text style is set to BRIGHT for the text, ‘Bright text’. In the same way, the style is set to NORMAL for the text, ‘Normal Text’ after resetting all previous color and style settings.

#Import required module
from colorama import Fore, Back, Style

#Print text using font color
print(Fore.CYAN + 'Welcome to Linuxhint')
#Print text using background color and DIM style
print(Back.YELLOW + Style.DIM + 'Learn Python', end='')
#Reset all style
print(Style.RESET_ALL)
#Print text using font color and BRIGHT style
print(Fore.RED + Style.BRIGHT + 'Bright Text', end='')
#Print reset all style again
print(Style.RESET_ALL)
#Print text without any color and normal style
print(Style.NORMAL + 'Normal Text')

Output
The following output will appear after executing the above script. The output shows that the font color of the first text has been applied in the second text because no reset task was done. But the color and style setting for the third and fourth text has been done separately for using Style.RESET_ALL property.

Example 3: Print Color Text Without Colorama

Many other modules exist in Python to set the color and style for the terminal text. The termcolor module is one of them. You have to install the module before using it in the script. Run the following command to install the termcolor module.

$ pip3 install termcolor

After installation, create a Python file with the following script to set the background color and font color by using the termcolor module. The font color will be set to RED and the background color will be set to CYAN for the text, ‘Colored text using Python’ after executing this script.

#Import necessary module
from termcolor import colored

#Initialize a text variable
text = "Colored text using Python"
#Print the text with font and background colors
print(colored(text, 'red', 'on_cyan'))

Output
The following output will appear after executing the above script.

Example 4: Clear Terminal Screen

The terminal screen can be cleared by using ansi.clear.screen() function of Colorama module. Create a Python file with the following to clear the terminal screen by using the Colorama module.

#Import colorama
import colorama as cl

#Imitialalize colorama
cl.init()
#Clear the terminal screen
print(cl.ansi.clear_screen())

Output
The following output will appear after executing the above script.

Conclusion

The output of the terminal can be made more attractive and understandable for the users by using the Colorama module of Python. Different ways of coloring the text background and setting the style and color of the font of the terminal have been shown in this tutorial by using colorama and another module. I hope the Python users will be able to apply color and style to the terminal text after reading this tutorial.

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.