📜  python print in color - Python (1)

📅  最后修改于: 2023-12-03 14:46:03.005000             🧑  作者: Mango

Python Print in Color

As a programmer, you often need to print information on the console during the development process for testing or debugging purposes. By default, the standard print() function in Python displays text in the terminal in a plain black color. However, you may want to add some colors to make your output more visually appealing and easier to read. This guide will introduce different ways to achieve colorful printing in Python.

Table of Contents
Print Colored Text using ANSI Escape Sequences

One of the simplest ways to print colored text in Python is by using ANSI escape sequences. These are special character sequences that, when used with the appropriate code, modify the terminal's output.

Here's an example of how to print text in different colors using ANSI escape sequences:

print('\033[91m' + 'Red Text' + '\033[0m')
print('\033[92m' + 'Green Text' + '\033[0m')
print('\033[94m' + 'Blue Text' + '\033[0m')

In the above code, the escape sequence \033[ is used to start the color format, followed by the desired color code (e.g., 91 for red, 92 for green, 94 for blue). Finally, \033[0m is used to reset the color back to the default.

Using Colorama Library

Colorama is a Python library that makes cross-platform colored terminal text output easy. It automatically handles ANSI escape sequences (used to add colors) on Windows, macOS, and Linux.

To use Colorama, first, install it using pip:

$ pip install colorama

Here's an example of how to print text in different colors using Colorama:

from colorama import Fore, Back, Style

print(Fore.RED + 'Red Text' + Style.RESET_ALL)
print(Fore.GREEN + 'Green Text' + Style.RESET_ALL)
print(Back.BLUE + 'Blue Background' + Style.RESET_ALL)

In the above code, we import Fore for foreground (text) colors, Back for background colors, and Style to reset any formatting.

Using Termcolor Library

Termcolor is another Python library that provides basic ANSI color formatting for output in terminals. It offers a simple cross-platform solution for colored text.

To use Termcolor, install it using pip:

$ pip install termcolor

Here's an example of how to print text in different colors using Termcolor:

from termcolor import colored

print(colored('Red Text', 'red'))
print(colored('Green Text', 'green'))
print(colored('Blue Text', 'blue'))

In the above code, we use the colored() function from Termcolor and pass the desired text along with the color as arguments.

Using Rich Library

Rich is a feature-rich library for beautiful terminal output in Python. It not only supports colored text but also offers a variety of other formatting options, including tables, progress bars, syntax highlighting, and more.

To use Rich, install it using pip:

$ pip install rich

Here's an example of how to print text in different colors using Rich:

from rich import print

print("[bold red]Red Text[/bold red]")
print("[bold green]Green Text[/bold green]")
print("[bold blue]Blue Text[/bold blue]")

In the above code, we use the print() function from Rich library and specify the desired color using the syntax [attribute color]Text[/attribute color].

Conclusion

Printing colored text in Python can enhance the readability and aesthetics of your console output. You can achieve this by utilizing ANSI escape sequences directly, or by using third-party libraries like Colorama, Termcolor, or Rich. Choose the method that best suits your needs and make your printing more vibrant and visually appealing. Happy coding!

*Note: Make sure to install the required libraries before running the code snippets.