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 colorama
.
PIP Installation (Windows)
pip install colorama
PIP Installation (Linux)
sudo pip3 install colorama
Anaconda Installation
conda install -c anaconda colorama
After installation, you can import colorama
into your Python Project.
Importing the Module
import colorama
How to use Python Colorama
Initializing 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 |
---|---|
init(autoreset = True) | Setting this to True will reset color and style settings after each line. |
init(strip = None) | Setting this to True will remove ANSI codes from the output. The default behavior is to strip if on Windows or if the output is redirected. |
init(convert = None) | Passing True will convert ANSI codes in the output into win32 calls. The default behavior is to convert if on Windows, and output is to a terminal. |
init(wrap = True) | On Windows, Colorama works by replacing sys.stdout and sys.stderr with proxy objects, which override the .write() method to do their work. If this wrapping causes you problems, then this can be disabled by passing init(wrap=False) . The default behavior is to wrap if auto-reset or strip or convert are True . |
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")
Output
This provides us with blue text on a white background.
Available Console Colors
These are all the colors Colorama
provides:
Fore.RED
Fore.GREEN
Fore.YELLOW
Fore.BLUE
Fore.MAGENTA
Fore.CYAN
Fore.WHITE
Applying Bold Style Settings
There are three text styles you can apply that effect text opacity.
- DIM
- NORMAL
- BRIGHT
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.
Output
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.
Output
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 Colorama
.
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
Python termcolor
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"))
Output
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:
#!usr/bin/env python
Alternatives to Colorama
Here’s a rundown of alternatives to the colorama
module. Links for their repos are also provided.
Python termcolor
vs Python colorama
Despite both functions working in conjunction with each other, here are a few differences.
termcolor | colorama |
---|---|
termcolor is a python module for ANSII Color formatting for output in the terminal. | 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
Conclusion
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.