Skip to content

Print Colored Text to the Terminal in Python AlixaProDev Spark By {Examples}

How to print colored text to the terminal in Python? There are several ways to print colored text to the terminal. Whether you want to use basic ANSI escape sequences or more advanced libraries like Colorama, Termcolor, or Rich. In this article, we will discuss the different methods for printing colored text to the terminal  Read More Python, Python Tutorial, Python Printing Spark By {Examples} 

How to print colored text to the terminal in Python? There are several ways to print colored text to the terminal. Whether you want to use basic ANSI escape sequences or more advanced libraries like Colorama, Termcolor, or Rich. In this article, we will discuss the different methods for printing colored text to the terminal and provide code examples for each.

Have you ever looked at the plain, black and white text output in your terminal and wished you could add some color to make it more visually appealing or easier to read? Let’s get started with quick examples.

1. Quick Examples – Print Colored Text to the Terminal

These examples will give a quick overview of the different ways to print colored text to the terminal, we will discuss each method in more detail in the following sections.

# Quick examples of Print Colored Text to the Terminal

# Using ANSI escape sequences
print(“33[31mRed text33[0m”)
# prints “Red text” in red

# Using the Colorama library
from colorama import init, Fore, Style
# initialize colorama on Windows
init()
# prints “Green text” in Green
print(Fore.GREEN + “Green text” + Style.RESET_ALL)

# Using the Termcolor library
from termcolor import colored
print(colored(“Blue text”, “blue”))
# prints “Blue text” in Blue

2. ANSI Escape Sequences to Add Color to Terminal Output

ANSI escape sequences are a set of characters used to control text formatting, colors, and other output options in the terminal. These sequences begin with the escape character 33, followed by optional parameters and a command character.

The escape sequence for setting the text color with ANSI escape codes consists of three parts: the escape character (33 or x1b), the command character ([), and one or more optional parameters separated by semicolons, followed by the letter m.

See the following Example

# Print color text
print(“33[31mRed text33[0m”)

The parameter values that can be used with the m command characters to set the text color are:

30 – Black

31 – Red

32 – Green

33 – Yellow

34 – Blue

35 – Magenta

36 – Cyan

37 – White

print(“33[31mRed text33[0m”)
print(“33[32mGreen text33[0m”)
print(“33[1mBold text33[0m”)
print(“33[4mUnderlined text33[0m”)

To reset the color back to the default, you can use the escape sequence 33[0m. The reset sequence must be included at the end of the colored text output, otherwise, all subsequent text output will also be colored. The above example yields the below output.

3. Terminal Colored Text – Colorama Library

The Colorama library is a Python package that makes it easy to print colored text and output to the terminal on Windows, macOS, and Linux systems. It provides a simple and consistent API for printing colored text, with support for a wide range of colors and formatting options.

To use the Colorama library in your Python code, you must first install it using pip:

# Install colorama module
pip install colorama

Use the Colorama library to print text with different colors and formatting options:

# Import colorama
from colorama import init, Fore, Style

# initialize
init()

# Colorama examples
print(Fore.RED + “Red text” + Style.RESET_ALL)
print(Fore.GREEN + “Green text” + Style.RESET_ALL)
print(Style.BRIGHT + “Bold text” + Style.RESET_ALL)

4. Termcolor Library – Colored Terminal Output

The Termcolor library is another popular Python package for printing colored text to the terminal. It provides a simple API for setting the foreground and background colors of text, as well as adding text attributes such as bold, underline, and blink.

# Install termcolor
pip install termcolor

See the following Examples:

# Import termcolor module
from termcolor import colored

# termcolor example
print(colored(“Red text on white background”, “red”, “on_white”))
print(colored(“Green bold text”, “green”, attrs=[“bold”]))
print(colored(“Yellow underlined text”, “yellow”, attrs=[“underline”]))

5. Terminal Black Text with Red Background

To print black color text with a red background using the termcolor library, we need to use the colored function and pass the text we want to print as the first argument, followed by the color we want to use for the text (in this case, “black”), and the color we want to use for the background (in this case, “on_red”).

See the following Example:

from termcolor import colored

print(colored(‘SparkByExample!’, ‘black’, ‘on_red’))

6. Summary and Conclusion

We have covered several ways to print colored text to the terminal in Python. From the basic approach of using ANSI escape sequences to the more advanced methods of using libraries like Colorama and Termcolor, we’ve covered a range of techniques for adding color to your terminal output. If you have any questions please don’t hesitate to leave a comment below.

Happy coding!

 

Leave a Reply

Your email address will not be published. Required fields are marked *