# Install the termcolor package !pip install termcolor # Import the colored() function from termcolor import colored # Print some coloured text print(colored("Hello, world!", color="red")) print(colored("Hello, world!", color="green", on_color="on_white"))
To print coloured text on the terminal in Python, you can use the colored() function from the termcolor package. This function allows you to specify the color of the text and the background, as well as other formatting options such as bold and underline.
In this example, we first install the termcolor package using pip. Then, we import the colored() function from the termcolor package. Finally, we use the colored() function to print some coloured text on the terminal.The colored() function takes the text to print as the first argument, and several optional arguments to specify the color and formatting of the text. In this example, we specify the color argument to set the color of the text, and the on_color argument to set the color of the background. There are several colors available, including red, green, blue, yellow, and many others.
Overall, the colored() function from the termcolor package allows you to print coloured text on the terminal in Python. It provides a simple and easy-to-use interface for setting the color and formatting of the text, and is compatible with most terminals and shell environments.
Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.