Is there a way we can print colors onto our console via Python? Yes, the most straightforward way is to use Python Colorama. macOS and Linux have their own inbuilt console color codes that do not work on Windows. This module makes sure those commands work on Windows and provides its own set of commands for console color settings.
What is Colorama?
Colorama is a Python module that displays colored output in consoles. Any text that is shown in the console can have its foreground and background changed.
macOS and Linux have their own inbuilt console color codes that do not work on Windows. This module makes sure those commands work on Windows as well.
You can check out Colorama public GitHub repo here.
Setting up Colorama
As of Python 3.10, this module is NOT a part of the Python Standard Library. To install manually, use PIP or Anaconda.
Running any of these commands will automatically install the latest version of
PIP Installation (Windows)
pip install colorama
PIP Installation (Linux)
sudo pip3 install colorama
conda install -c anaconda colorama
After installation, you can import
colorama into your Python Project.
Importing the Module
How to use Python Colorama
Our first step is to initialize
Colorama by running the
init() method, as follows:
import colorama colorama.init()
init() can take some keyword arguments, here’s a rundown:
|Keyword Argument + Default Value||Description|
|Setting this to |
|Setting this to |
|On Windows, Colorama works by replacing |
Writing Colored Text
The following program demonstrates how we can write colored text and background.
from colorama import init, Fore, Back init() # Fore changes the text's foreground color print(Fore.BLUE + "Blue Letters") #Back changes the text's background color print(Back.WHITE + "White Background")
This provides us with blue text on a white background.
Available Console Colors
These are all the colors
Applying Bold Style Settings
There are three text styles you can apply that effect text opacity.
from colorama import init, Fore, Style init() # Prints text with minimum opacity print(Fore.BLUE + Style.DIM + "Blue Letters") # Prints text with default console opacity print(Fore.BLUE + Style.NORMAL + "Blue Letters") # Prints text with high opacity (Bold letters) print(Fore.BLUE + Style.BRIGHT + "Blue Letters")
Note: These settings may not be visible on all terminals.
Resetting the Color/Style Settings
You can reset all the colors and style settings by simply passing
Style.RESET\_ALL(). Running this method will reset everything back to default console settings.
from colorama import init, Fore, Back, Style init() print(Fore.BLUE + Back.WHITE + "Sample Text" + Style.RESET\_ALL) print("Reset to default settings")
If you’re constantly going to be using different color settings, you can enable auto-reset upon initialization like this:
colorama.init(autoreset = True)
This resets to default settings after the execution of each line.
Adjusting Console Font Size
This is unfortunately not possible. Python has no control over the size of the printed text. That’s entirely dependent on the settings of your terminal emulator.
The only way to print out large letters onto your console is to use ASCII Graphics.
Implementing Blinking Console Text
Here’s a way you add Blinking text using
import colorama colorama.init() text=colored("HI THERE!", color="magenta", on_color="on_cyan", attrs=["blink"]) print(text)
Note that Windows terminals do not support blinking text.
Clearing Console Screen
Here’s an example for using the
colorama to clear your output console screen.
import colorama colorama.init() print(cr.ansi.clear_screen())
Other Methods to Implement Console Color Formatting
Termcolor is another module that allows similar color formatting for console outputs. It’s also an external module. You should install manually using PIP or Anaconda.
import colorama from termcolor import colored colorama.init() print(colored("red color", "red")) print(colored("yellow color", "yellow")) print(colored("blue color", "blue")) print(colored("cyan color", "cyan")) print(colored("green color", "green")) print(colored("magenta color", "magenta"))
Common Errors in Colorama
Requirement already satisfied: colorama in /usr/lib/python2.7/dist-packages.
Make sure your header code’s Python Version matches the one you’re currently Using.
If you’re using Python 2, your header code should look like this:
Alternatives to Colorama
Here’s a rundown of alternatives to the
colorama module. Links for their repos are also provided.
termcolor vs Python
Despite both functions working in conjunction with each other, here are a few differences.
|Cross-platform printing of colored text can be done using’s constant shorthand for ANSI escape sequences.|
|Only provides color formatting for console display.||Provides attributes that allow you to strip, convert and wrap ANSI codes.|
|It can be installed using PIP/Anaconda. Not part of the Standard Library||It can be installed using PIP/Anaconda. Not part of the Standard Library|
FAQs on Python Colorama
Similar to Linux, running:
sudo -H python3 -m pip install colorama
will install colorama for Ubuntu OS
We have looked at how to implement Python Colorama to color format console outputs. An alternative to Colorama has been reviewed. Style settings to change the console text appearance have been evaluated. This is a must-use module if you’re working with results within your system console.