In this post we will be creating Python script that will change the 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 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
Fore class in Colorama, followed by the text to print, and ends with
Fore.RESET to reset the text color to the default color.
The first message is printed in red text, the second in green text, and the third in yellow text. Finally, the code calls
time.sleep() to pause execution for 10 seconds before exiting.
import colorama from colorama import Fore import time colorama.init() print(Fore.RED + 'This text is red!' + Fore.RESET) print(Fore.GREEN + 'This text is green!' + Fore.RESET) print(Fore.YELLOW + 'This text is yellow!' + Fore.RESET) 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.