Javatpoint Logo
Javatpoint Logo

How to Print Colored Text in Python

In this tutorial, we will learn how to print colored text in Python. The command line applications get the same color as the terminal. Sometimes, we want to print text in output to get the user's attention. In certain circumstances, a splash of color may make all the difference, and Python provides the easiest way. The programmers use colorful text to get the user's attraction for some specific messages.

Print Colored Text in the Terminal using Simple Color

Python provides an excellent "simple color" module that permits us to show colorful text on the terminal. It can print text in various colors, such as blue, green, magenta, red, yellow, and many more.

It also allows us to apply many styles, such as bold, dim, italic, brilliant, underlined, reverse, and blink. The simple-color module is not included in the Python package, so we need to install it before using it. To install it, copy the below command and paste it to the terminal.

Or

Once the module is installed, we can customize/style of our code using the syntax shown in the sample below.

Example -

Output:

How to Print Colored Text in Python

Explanation -

In the above code, we utilized the specified technique and gave the kind of formatting (bold, italic, underline, etc.) as an input to the color function to apply styles and colors to the text. We can provide several formatting styles to the function by packing within a list.

Print Colored Text Output using ANSI Escape Sequence

We can use the ANSI escape codes to modify the color of the text output in a Python application, which enhances the code readability and texture. It is quite useful in highlighting mistakes. The print statement is immediately updated with the escape codes.

In the terminals, two color schemes are commonly used:

  1. 16 colors (8 for the background and 8 for the foreground)
  2. 256 different colors

The 16-color option and Color for Output

ESCESQ STYLE
Escape Codes Text Style
HexaDecimal Normal: 0
\x1b[ Bold: 1
Unicode Light: 2
\u001b[ Italicized: 3
Octal: Underlined: 4
\033[ Blink: 5

To put this to the test, print a messy color pattern with red solid writing and a yellow backdrop. Bold text is signified by style code 2. The color codes for the red text in the foreground are 31 and 43 for the yellow backdrop. With that in mind, the syntax for describing this layout is as follows:

256 Different Colors

We can dive in right after we understand the syntax of a 256-color scheme. Working with 256 colors is slightly different from working with the 16-color scheme.

Some of the terminal provides the facility to specific escape sequence to change the printed material's tone, color, and look. These escape sequences are known as ANSI escape sequences because of the ANSI standard specifies their application.

Let's understand the following example -

Example -

Let's understand the following example -

Example -

Output:

How to Print Colored Text in Python

Print Colored Text using Termcolor Module

Python provides another library for colored text called Termcolor, which uses ANSII color formatting.

This module allows text formatting attributes as well as different properties for various terminals. In Addition, it contains multiple font colors, such as blue, red and green, as well as text highlights like magenta, cyan, and white.

As the simple-color library, we need to install it because it is not included in the core Python library. To install it, type the following command in the terminal.

Let's understand the following code.

Example - 1:

Example - 2:

Output:

How to Print Colored Text in Python

Example - 3:

Output:

How to Print Colored Text in Python

Print Colored Text using Colorma Module

Colorma does nothing on other platforms. Window will replace the ANSI escape sequence by calling init(). The init leaves Colorama before the program ends(). It will set stdout and stderr back to their original values. To install it, type the following command in the terminal and press enter.

Let's understand the following example -

Example -

Output:

How to Print Colored Text in Python

Let's see another example -

Example -

Output:

How to Print Colored Text in Python

Print Colored Text using colored Module

It is an elementary-colored package used to format and color text on terminals. It must first be installed using pip because it is not a built-in module. Let's see the following command.

Let's understand the following example.

Example -

Print Colored Text using HTML Object

A print formatted text() method in the Prompt toolkit is compatible with the built-in print() function. It allows us to format; colors and we can show the string HTML-based formatting using HTML. The HTML object understands the <b>, <i>, and <u> tags, which are required for bold, italic, and underline.

We need to install the package using the following command.

Let's understand the following example -

Example -

Output:

Hello Welcome to Javatpoint

Hello Welcome to Javatpoint

Hello Welcome to Javatpoint

Example -

Output:

How to Print Colored Text in Python

As all the HTML tags are mapped to classes from a style sheet, you can also appoint a style for a custom tag.

Conclusion

This tutorial included how to print the colored text in the output. This library is beneficial to attract the user's attention, or it can be used to highlight some specific errors.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA