DEV Community

Vishesh Dvivedi
Vishesh Dvivedi

Posted on

Get Colored Console Output In Python Using Colorama

In this blog I am gonna talk about a python module that can make your console based applications look much better than they normally look, the colorama module.

Here's how a normal console application looks without using colorama.

coloring using<br>
colorama

And here's how the same script would look with some color effects.

how\_to\_get\_colored\_output

Isn't that beautiful !!!

All of this was possible due to colorama module. And we are gonna learn how we can use this module in python.

About The Module

Colorama is a python module that is used to display colored output in console. It can change both, foreground and background color of any text which is displayed in the console.

The link to its github repository is this:
colorama.

colorama<br>
github

Getting Started With Colorama

First you need to download and install colorama. Just open your command prompt and type the following:

For Windows:

pip install colorama
Enter fullscreen mode Exit fullscreen mode

For Linux:

sudo pip3 install colorama
Enter fullscreen mode Exit fullscreen mode

This will automatically download and install colorama in your system.

(Note: you should have pip installed in your system for this to work, If it is not, check out this blog and setup your pip)

Once your colorama is installed, you can import colorama in any of your python script and use it. Now let's learn how to use it within our scripts.

Using colorama

1. Initializing colorama

First we have to initialize colorama by running the init method, like this:

from colorama import init
init()
Enter fullscreen mode Exit fullscreen mode

On Windows, calling init() will filter ANSI escape sequences out of any text sent to stdout or stderr, and replace them with equivalent Win32 calls.

On other platforms, it's not of much use. So don't forget to call this function is your script runs on Windows.

2. Colored Output

It's time we write our first colored output on the screen, like this:

from colorama import init, Fore, Back, Style

init()
print(" Normal white color")
print(Fore.RED + " Letters will be in red ")
print(Back.GREEN + " Background will be green " + 
Style.RESET\_ALL)
print(Style.DIM + Fore.RED + Back.GREEN + " And style will be 
dim " + Style.RESET\_ALL)
print(" And everything is back to normal")
Enter fullscreen mode Exit fullscreen mode

The output would look like this:

colorama<br>
output

As you might understand, you can change the foreground letter color using the Fore class and selecting the correct color constant you want to use. You can change background color using Back class and change style of letters using Style class.

Here are the possible foreground, background and style class.


 {style="background-color: #f9f9f9; border: 1px solid rgb(211, 211, 211); box-sizing: inherit; color: #6c6c6c; font-family: "Source Code Pro", monospace; font-size: 0.85rem; margin-bottom: 0px; margin-top: 30px; overflow: auto; padding: 15px;"}
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


Enter fullscreen mode Exit fullscreen mode

Note: The color effects that you will place will remain consistent, unless you call deinit() function or  mention Fore.RESET, Back.RESET or Style.RESET_ALL.

3. Using termcolor

You can also use termcolor (in Windows) to color a specific portion of the console output

from colorama import init
from termcolor import colored

init()

print(colored("This is in red color", "red"))
print(colored("This is in yellow color", "yellow"))
print(colored("This is in blue color", "blue"))
print(colored("This is in cyan color", "cyan"))
print(colored("This is in green color", "green"))
print(colored("This is in magenta color", "magenta"))
Enter fullscreen mode Exit fullscreen mode

colorama<br>
colors

just import colored function from termcolor and use it as following:

print( colored( "string to color",  "color name" ) )
Enter fullscreen mode Exit fullscreen mode




Conclusion

Overall this is a very awesome module and I bet you should definitely try using this module within your scripts, to give it a more professional look and feel.

Hope this blog helped you all :)

You can follow me on
YouTube,
Instagram,
Linkedin.

Top comments (1)

Collapse
 
kwarfscode profile image
Addae Kwarfo

My PC doesn't support coloroma