Register Login

How to print colored text in Python?

Updated Sep 18, 2022

We all want to make our content look attractive and well-readable to our users. For this, Python users can use some built-in modules to print the colored text output of the script in the terminal.

Users can generate colored text output using different ways, such as using the termcolor Module, Colorama Module, and ANSI Escape Sequence. This article will briefly discuss the three methods mentioned above to print colored text in Python.

Method 1: Using the Termcolor Module in Python:

The Termcolor module is a python module that helps to format ANSII Color for output in the terminal. All a user needs to do is to install the termcolor module and use it in the Python script.

Code:

import sys
from termcolor import colored, cprint
text = colored('Hey, this is me', 'blue', attrs=['reverse', 'blink'])
print(text)

Output:

Explanation:

In the above code snippet, we have used the termcolor module to print the text with the required color. In the output console, the interpreter shows the color blue.

Another example of the termcolor module is:

import sys
from termcolor import colored, cprint
text = colored('This is Python', blue, attrs=['reverse', 'blink'])
print(text)
cprint('Hello, World!', 'green', 'on_red')
def print_red_on_cyan(x): return cprint(x, 'red', 'on_cyan')
print_red_on_cyan('This, is, Python')
print_red_on_cyan('Hey, Python!')
for i in range(20):
   cprint(i, 'blue', end=' ')
cprint("Alert!", 'green', attrs=['bold'], file=sys.stderr)

Output:

Explanation:

Here, we have used the built-in module of Python with for loop to print the numbers from 0 to 19 and create colored text in Python.

Method 2: Generate colored text using the Colorama module:

Colorama module is a Cross-platform API for printing colored text in Python. Users can use it to display colored output in the console and can do coding using Colorama's constant shorthand for ANSI escape sequences.

The built-in module can change the background and the foreground color of any text, which the Python interpreter displays in the console.

Users first have to import the functions, namely, init(), which help in initializing the module by setting the autoreset to "True," so they do not have to reset it manually. The Fore function provides the Foreground text object, the Back function sets the Background Object, and Style for Style Object.

Code Snippet:

from colorama import Fore, Back, Style
print(Fore.BLACK + 'some black text')
print(Back.BLUE + 'and with a blue background')
print(Style.DIM + 'here are some dim text')
print(Style.RESET_ALL)
print('back to normal now')

Output:

Explanation:

Using the colorama built-in module, we are importing three Python functions and using these functions to print colored text. These are Fore, Back, and Style.

Code Snippet:

from colorama import init
from termcolor import colored
init()
print(colored("Hey, this is cyan Python", "cyan"))
print(colored("Hey, this is yellow Python", "yellow"))
print(colored("Hey, this is magenta Python", "magenta"))
print(colored("Hey, this is green Python", "green"))
print(colored("Hey, this is blue Python", "blue"))
print(colored("Hey, this is red Python", "red"))

Output:

Explanation:

Here, in the above code snippet, we have used the two built-in modules in Python, colorama, and termcolor.

Method 3: Using ANSI Code to print colored text in Python:

Users can also use the ANSI Escape Sequences or Codes to print colored texts in Python. These are special strings that change the working of the internal terminal.

A typical example compared with the ANSI Code would be the \n character, which is a New Line sequence. When users use this in the code, the interpreter does not print it in the console but prints a new line in the output.

The interpreter generates a colored text on the terminal based on ANSI Escape sequences.

Code Snippet:

def prRed(skk): print("\033[91m {}\033[00m" .format(skk))
def prCyan(skk): print("\033[96m {}\033[00m" .format(skk))
def prGreen(skk): print("\033[92m {}\033[00m" .format(skk))
def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk))
def prYellow(skk): print("\033[93m {}\033[00m" .format(skk))
def prLightPurple(skk): print("\033[94m {}\033[00m" .format(skk))
def prBlack(skk): print("\033[98m {}\033[00m" .format(skk))
def prPurple(skk): print("\033[95m {}\033[00m" .format(skk))
prCyan("This is Python")
prGreen("Ruby")
prYellow("Hey, this is Python")
prRed("HTML")
prGreen("C++")

Output:

Explanation:

The above code snippet shows how to use the ANSI Escape sequences to print the colored text with different color combinations.

Conclusion:

In this tutorial, we have gone over how to print colored text in Python output console for the characters users send off to the stdout stream.

Also, we have explored the different methods; and their code snippets, showing how they works and using the built-in functionality Python offers, as well as how to use the Colorama and Termcolor Built-in module. The first two methods are the efficient ones.


×