Advanced Methods: Ultimate Guide Python Colorama

About The Module of Python Colorama

Python Colorama may be a python module that’s wont to display colored output in console. It can change both, foreground and background color of any text which is displayed within the console.

pip install colorama

Or Install From Here

 

Started With Colorama

First you wish to download and install colorama. Just open your electronic communication and kind the following:

For Windows:

pip install colorama 

For Linux:

sudo pip3 install colorama

Once your python colorama is installed, you’ll import colorama in any of your python script and use it. Now let’s learn the way to use it within our scripts.

 

Using colorama

1. Initializing colorama

First we’ve to initialize colorama by running the init method:

from colorama import init
init()

For Windows, calling init() will filter full of ANSI escape sequences, out of any text letters sent to stdout or stderr(sys), and replace with the equivalent Win32 calls.
On other platforms python colorama, it is not of much use. So remember 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") 

The output would look like this:

colorama full guide

 

As you would possibly understand, python colorama you’ll be able to change the foreground letter color using the Fore class and selecting the right color constant you wish to use. you’ll change background color using Back class and alter type of letters using Style class.

Here are the possible foreground, background and elegance class.

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 

Note: the python colorama colour effects that you simply will place will remain consistent, unless you call deinit() function or mention Fore.RESET, Back.RESET or Style.RESET_ALL.

 

3. Using termcolor

To use termcolor (in Windows) to colored a particular 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"))

OUTPUT

Advanced Methods: Ultimate Guide Python Colorama

import colored function from termcolor

print( colored( "string to color",  "color name" ) )