Python 5d - COlorama

What is Colorama?

Colorama is a library that allows the colour of text to be changed.

Information about the library can be found on the Python Package Index (PyPi) website. Copyright of the library is held by Jonathan Hartley & Arnon Yaari.

                  Colorama can be imported when using an online editor like Replit.

             

                  Colorama is not available as a default library on the standard Python offline editor.

Using Colorama

The three main commands using Colorama are:

  • Fore to change the text colour.

  • Back to change the highlight colour.

  • Style to make the text appear dim or bright.

from colorama import Fore

print(Fore.GREEN + "Hello There")

Hello There

from colorama import Back

print(Back.YELLOW + "Goodbye Now")

Goodbye Now

from colorama import Style

print(Style.DIM + "Hi Again")

Hi Again

There are 8 possible colours to choose with the Fore and Back commands. You must write the colour name in CAPITAL LETTERS.

BLACK        CYAN        GREEN        MAGENTA        RED        WHITE        YELLOW                       There is also the RESET option, e.g. Fore.RESET

The 2 options to choose with the Style command are DIM and BRIGHT. You can also use Style.RESET_ALL

Practice Task

Create a simple traffic light program. 

The user is prompted for an input.

 

Typing GO will output a suitable message in GREEN, typing WAIT will output a message in YELLOW and typing STOP will output a response in RED.

Example solution:

What should the driver do? STOP

You must stop your car.

What should the driver do? GO

It is safe to continue driving.