Color Python output text

less than 1 minute read

Published:

Output text of the Python can look too monotnous as it lacks the color. In order fill life into that, you can use termcolor module of Python that helps you color the output text.

In order to use termcolor module, you need to first install it using terminal. Use the command:

pip install termcolor

Once it is installed, you can use the colored() function to print coloured output.

Syntax: colored(output_text, color, highlights, attribute = [])

output_text is the string that will be displayed. color is the color code of the text, highlights is the optional argument for background color and attribute is optional argument to highlight text.

Example:

from termcolor import colored

text = colored('Hello, World!', 'red',attrs=[ 'blink'])
text_highlighted = colored('Hello, World!', 'red','on_cyan')
print(text)
print(text_highlighted)

Output:

References