Python 5d – Colorama

Add colourful text, highlights, and styles to your console apps with colorama.

What is Colorama?

Colorama lets you change text colour, background colour, and brightness in terminal programs. It’s commonly available in online editors (e.g. Replit), but for local editors like IDLE you may need to install it first.

Install (for offline/IDLE)

Install the package via pip if it’s not already available:

pip install colorama

Using Fore, Back, and Style

Import the parts you need and print coloured/styled text. Available colours include: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE. Styles include DIM and BRIGHT.

from colorama import Fore, Back, Style

print(Fore.GREEN + "Hello There")
print(Back.YELLOW + "Goodbye Now")
print(Style.DIM + "Hi Again")

Tip: When you concatenate colour/style with strings, those settings stay “on” until you reset them (see below).

Reset colours & autoreset

Use Style.RESET_ALL to clear any colours/styles, or enable autoreset so each print resets automatically.

from colorama import Fore, Back, Style, init

# Option 1: Manual reset
print(Fore.RED + "Danger!" + Style.RESET_ALL)
print("Back to normal text.")

# Option 2: Auto reset after every print
init(autoreset=True)
print(Fore.CYAN + "This line is cyan.")
print("Colours reset automatically after each print.")

Colorama Task – Traffic Lights

Ask the user what the driver should do. Print a coloured instruction: GO → green, WAIT → yellow, STOP → red.

from colorama import Fore, init

init(autoreset=True)

action = input("What should the driver do? ").upper()

if action == "GO":
    print(Fore.GREEN + "It is safe to continue driving.")
elif action == "WAIT":
    print(Fore.YELLOW + "Please wait for a moment.")
elif action == "STOP":
    print(Fore.RED + "You must stop your car.")
else:
    print("Invalid input!")