Python print colored message on command line terminal window

When you are developing a python script with some output messages printed on the terminal window, you may find a little bit boring that all the messages are printed in black and white, especially if some messages are meant for warning, and some just for information only. You may wonder how to use different colors to make them look differently, so that your users are able to pay special attention to those warning or error messages.

In this article, I will be sharing with you a library which allows you to print your output messages in different colors.

Let’s get started!

The library I am going to introduce called colorama, which is a small and clean library for coloring your messages in both Windows, Linux and Mac os.

Prerequisite

You will need to install this library, so that you will be able to run the following code in this article.

pip install colorama

To start using this library, you will need to import the modules, and call the init() method at the beginning of your script or your class initialization method.

import colorama
from colorama import Fore, Back, Style
colorama.init()

The init method also accepts some **kwargs to overwrite it’s default behaviors. E.g. by default, the style will not be reset back after printing out a message, and the subsequent messages will be following the same styles. You can pass in autoreset = true to the init method, so that the style will be reset after each printing statement.

Below are the options you can use when formatting the font, background and style.

Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Style: DIM, NORMAL, BRIGHT, RESET_ALL

To use it in your message, you can do as per below to wrap your messages with the styles:

print(Fore.CYAN + "Cyan messages will be printed out just for info only" + Style.RESET_ALL)
print(Fore.RED + "Red messages are meant to be to warning or error" + Style.RESET_ALL)
print(Fore.YELLOW + Back.GREEN + "Yellow messages are debugging info" + Style.RESET_ALL)

This is how it would look like in your terminal:

As I mentioned earlier, if you don’t set the autoreset to true, you will need to reset the style at the end of your each message, so that different message applies different styles.

What if you want to apply the styles when asking user’s input ? Let’s see an example:

print(Fore.YELLOW)
choice = input("Enter YES to confrim:")
print(Style.RESET_ALL)
if str.upper(choice) in ["YES",'Y']:
print(Fore.GREEN + "You have just confirmed to proceed." + Style.RESET_ALL)
else:
print(Fore.RED + "You did not enter yes, let's stop here" + Style.RESET_ALL)

By wrapping the input inside Fore.YELLOW and Style.RESET_ALL, whatever output messages from your script or user entry, the same style will be applied.

Let’s put all the above into a script and run it in the terminal to check how it looks like.

Yes, that’s exactly what we want to achieve! Now you can wrap your printing statement into a method e.g.: print_colored_message, so that you do not need to repeat the code everywhere.

As per always, please share if you have any comments or questions.

Original post from: https://www.codeforests.com

--

--

--

Resources and tutorials for python, data science and automation solutions

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Smartlink Community Awards New Winner

Smartlink Community Awards Winner

Deploy a simple webpage with EFS serving as the permanent storage backend

8 Key Components of a Perfect CIAM Platform

Docker run/exec and carriage return

Migrating a CloudFlare website to EasyWP

Working in the seams of code with Resource Locator

How to Create a Dynamic Library in C

A simple IoT architecture to Ingestion and Reporting

architecture big picture

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
codeforests

codeforests

Resources and tutorials for python, data science and automation solutions

More from Medium

Python multiprocessing and why it’s not always the best solution

Python 3.11: Unpacking Five New Features.

How to Use UTF-8 with Python

Python: Grouping data with itertools