Print Colored Text to the Terminal in Python

Colored Text To Terminal

We can do many exciting things in Python, making learning fun and more enjoyable. One of them is printing the colored text to the terminal. We can represent the values with the different colors in the text. In this article we discuss some of the libraries in pythong can help us achieve this goal.

Let’s begin our colorful journey by printing the results in colored text.

Colored Text Using “termcolor”

“termcolor” is an ANSI escape sequence used for color formatting for output in the terminal.  These let you add colors, colored backgrounds, or decorations to your printed text. This does not do any fancy work, it is just to print text with colors and backgrounds.

Installation

To use this package, we first need to install it by using the pip command.

pip install termcolor

Example:

from termcolor import colored, cprint

print("Hello World!!!", 'red')
cprint("Hello World!!!", 'red')
print(colored("Hello World!!!", 'light_cyan'))
print(colored("Hello World!!!", 'green', attrs=['underline']))
  1. import colored and cprint from the package “termcolor
  2. firstly we print the normal text “”Hello World!!!”, ‘red'”
  3. cprint is the function from the termcolor library to print the output in a certain color, as mentioned.
  4. the colored function has two arguments: first is our text, and another one is the color provided by the library
  5. lastly, we provide the attribute underlined with the color green and the text on which we want to apply it.
Termcolor Text
Termcolor Text

In the output, we are getting normal text, secondly, we are getting the red text as mentioned in the argument, then, we provide the light_cyan color and lastly, the underlined text with the color green.

Here are the colors, highlights, and attributes which can be used.

Colorred, black, light_cyan, green, magenta, white, etc
Highlightson_black, on_red, on_green, on_white, etc
attrsbold, dark, underline, blink, reverse, and concealed

Colored Text Using “colorama”

colorama is also used to show the color texts in the terminal. It is a cross-platform colored terminal text. This is also used to present the text in the colored format in the console. There are formatting constants available that can be used for color text, background color, and various styles.

Installation

Install the package to use its functions and methods.

pip install colorama

Example

from colorama import Fore, Back, Style

print(Fore.YELLOW + "Yellow Mellow")
print(Back.CYAN + 'Yellow text with cyan background')
print(Style.RESET_ALL)
print('Normal Text')

In this piece of code, we are importing Fore, Back, and Style from the colorama library, and the code follows:

  • printing some red text using Fore.YELLOW which will turn the text color yellow
  • then without resetting the text color, the text is printed with the cyan background
  • then we are resetting all the colors and backgrounds with Style.RESET_ALL property
  • then the normal text gets printing without any color.
Colorama Text 1
Colorama Text 1

In the output, we can see the text with colors and backgrounds. There are more available formatting constants.

Fore(text color)BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET
Back(for highlight)BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET
StyleDIM, NORMAL, BRIGHT, RESET_ALL

You may check this interesting topic: Python Modules vs Python Packages


Colored Text Using ANSI

There are Python modules especially suited for displaying colored text, ANSI is a no-dependency method used for color codes. ANSI escape sequence is in-band signaling to control locations of cursors and colors, to style the font, and much more. Let us take a look at how we can use ANSI to print the colored text to the console.

Example

print("\033[0;32m Green Color\n")
print("\033[0;34m Blue Color\n")
print("\033[1;32;40m Bright Green \n")
print("\033[0;31m Red \033[0m\n")

In this code, we are printing the colors using an ANSI escape sequence.

  • \033[ : Escape code like \n
  • 1: 1 is for the normal style.
  • 32: green text color code.
  • 40m: black background color.
  • \033[0: To reset the value
Ansi Color 1
Ansi Color

In the output, we can see the colored text according to the codes and colors we selected. We get the result by using the inbuilt ANSI.


Conclusion

In this article, we learned how we could console the colored output. We used “termcolor,” “colorama,” and “ANSI” to get the desired output. In termcolor and colorama, we have to import the library to use its functionalities, whereas ANSI is quick and requires no installation. These libraries representing data with particular text colors can be helpful. Red could be used for warnings, blue for the right input, and so on, according to the requirement.

You may also check: Python Pretty print JSON – A Complete Guide

References

  1. Termcolor
  2. Colorama