In this post we will be creating a Python script that will print the colors of the rainbow 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 print the colors of the rainbow. We start by creating a list of color codes that will be used to print the rainbow. The script then loops through each color in the “colors” list and prints a colored block character (represented by ‘█’) in that color. The “end” parameter is set to an empty string to prevent a new line from being printed after each block. A pause for 10 seconds is invoked before exiting, giving the user time to see the output before it disappears.
import colorama from colorama import Fore, Style import time colorama.init() colors = [Fore.RED, Fore.YELLOW, Fore.GREEN, Fore.CYAN, Fore.BLUE, Fore.MAGENTA] for color in colors: print(color + '█', end='') print(Style.RESET_ALL + Fore.WHITE + Style.BRIGHT + ' The rainbow.') 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.