In this post we will be creating Python script that will change the colour and background color of text in console using Colorama. Colorama is a Python library that simplifies the process of adding colors and styles to command-line output. It provides cross-platform support for colored terminal text, making it easier to enhance the visual appearance of terminal text. With Colorama, developers can easily make their terminal output more readable and visually appealing.
See the sample of Python code below where we use the Colorama library to change the colour and background color of text.
The code uses the print()
function to output three different messages with different colors. Each message uses a different color from the Back
class and Fore
class in Colorama, followed by the text to print, and ends with Style.RESET
_ALL to reset both the text and background text color to the default color.
The first message is printed in a green background with black text. The second in a blue background with white text , and the third in a magenta background with yellow text. Finally, the code calls time.sleep()
to pause execution for 10 seconds before exiting.
import colorama
from colorama import Fore, Back, Style
import time
colorama.init()
print(Fore.BLACK + Back.GREEN + 'This has a green background and black text!' + Style.RESET_ALL)
print(Fore.WHITE + Back.BLUE + 'This has a blue background and white text!' + Style.RESET_ALL)
print(Fore.YELLOW + Back.MAGENTA + 'This has a magenta background and yellow text!' + Style.RESET_ALL)
time.sleep(10)
See an image example below.

Take a look at some of our other content around the Python programming language by clicking here.