Python Program to Print Colored Text to the Terminal
Colored text refers to the practice of displaying text in different colors in order to convey additional meaning or to enhance its visual appeal. In Python, colored text can be achieved using various third-party modules such as termcolor, colorama, and colored.
Python Code :
The below program prints colored text to the terminal:
# Import necessary libraries from termcolor import colored # Example usage print(colored("Hello, World!", "red")) print(colored("This text is green on yellow background", "green", "on_yellow"))
In this program, we first import the colored function from the termcolor library. The colored function takes a string as its first argument and optional arguments for the text color and background color.
In the example usage, we use the colored function to print two different strings in different colors. The first string is printed in red text and the second string is printed in green text on a yellow background. Output:
This text is green on yellow background Note that the termcolor library is not part of the Python standard library, so it may need to be installed separately using a package manager like pip.