Get Colored Console Output In Python Using Colorama 
Tags: python, beginners, programming, tutorial
Published: 2021-11-22 06:25:36

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
colorama](https://1.bp.blogspot.com/-OI92mMfspec/YNn-I-V7dQI/AAAAAAAAAYo/ecepCARIsNMd4L6xsemnjOEfsaJPthjdgCLcBGAsYHQ/w640-h315/coloring_script_using_colorama.PNG "coloring using colorama")](https://1.bp.blogspot.com/-OI92mMfspec/YNn-I-V7dQI/AAAAAAAAAYo/ecepCARIsNMd4L6xsemnjOEfsaJPthjdgCLcBGAsYHQ/s1202/coloring_script_using_colorama.PNG)

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

[![how\_to\_get\_colored\_output](https://1.bp.blogspot.com/-1vQpuFKYtr4/YNqj1JgXODI/AAAAAAAAAY8/uwCiHwc2mFUretP0MhTazsDzQyd-NFfOgCLcBGAsYHQ/w640-h300/colorama_module.PNG "how_to_get_colored_output")](https://1.bp.blogspot.com/-1vQpuFKYtr4/YNqj1JgXODI/AAAAAAAAAY8/uwCiHwc2mFUretP0MhTazsDzQyd-NFfOgCLcBGAsYHQ/s1236/colorama_module.PNG)

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](https://github.com/tartley/colorama).

[![colorama
github](https://1.bp.blogspot.com/-erQzkaJRr5k/YNrcU4vnvNI/AAAAAAAAAZE/xSvIC8FHI6AhGeJgMMYTtUO51lFQ4WhUQCLcBGAsYHQ/w640-h294/colorama_github.PNG "colorama github")](https://1.bp.blogspot.com/-erQzkaJRr5k/YNrcU4vnvNI/AAAAAAAAAZE/xSvIC8FHI6AhGeJgMMYTtUO51lFQ4WhUQCLcBGAsYHQ/s1894/colorama_github.PNG)


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

For Linux:

    sudo pip3 install colorama

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](https://phoenixnap.com/kb/install-pip-windows) 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()


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")


The output would look like this:

[![colorama
output](https://1.bp.blogspot.com/-3krstzmJtdQ/YNrj-STHE_I/AAAAAAAAAZM/2kte2KCh-qgZtobxY3Xxeiyop1ViLmxLgCLcBGAsYHQ/w640-h252/colorama_output.PNG "colorama output")](https://1.bp.blogspot.com/-3krstzmJtdQ/YNrj-STHE_I/AAAAAAAAAZM/2kte2KCh-qgZtobxY3Xxeiyop1ViLmxLgCLcBGAsYHQ/s625/colorama_output.PNG)

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
```

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"))


[![colorama
colors](https://1.bp.blogspot.com/-rCP5iAFHFR0/YNsDr8McPzI/AAAAAAAAAZU/XkPiYRLcYCAUBAseE3PstVSHMR6R_mwOwCLcBGAsYHQ/w640-h424/colorama_colors.PNG "colorama colors")](https://1.bp.blogspot.com/-rCP5iAFHFR0/YNsDr8McPzI/AAAAAAAAAZU/XkPiYRLcYCAUBAseE3PstVSHMR6R_mwOwCLcBGAsYHQ/s305/colorama_colors.PNG)

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

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

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](https://www.youtube.com/channel/UCggZvARaczWC4wc4E6f330w?sub_confirmation=1),
[Instagram](https://www.instagram.com/itsallaboutpython),
[Linkedin](https://www.youtube.com/redirect?event=channel_banner&redir_token=QUFFLUhqazNFMkhoS05EZDBGYkV4bjB3Q2VUelp5M3JlZ3xBQ3Jtc0trVHdHUVMxY0RHYWtyRVRFbXBKZFBnWFV1VkVUazZVOW9zaE91Zi1qN3BkSFNOQjhlQXRhUE1JVkdfVGJtU2c5U3BXdGViZUhpRnNtbTdZeElQX2o5WGdtN0wxTm1fcjJOQ0NhMUVmWF9adUVJVThFVQ&q=https%3A%2F%2Fwww.linkedin.com%2Fin%2Fvishesh-dvivedi-07a82a20b%2F).