Built with Tutify.io

    AIStudyLabs

    Print Colored Text to the Terminal in Python

    Print Colored Text to the Terminal in Python

    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.

    <a id="h-1-quick-examples-print-colored-text-to-the-terminal"></a>

    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("\033[31mRed text\033[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
    

    <a id="h-2-ansi-escape-sequences-to-add-color-to-terminal-output"></a>

    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 \033, 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 (\033 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("\033[31mRed text\033[0m")
    

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

    
    print("\033[31mRed text\033[0m")
    print("\033[32mGreen text\033[0m")
    print("\033[1mBold text\033[0m")
    print("\033[4mUnderlined text\033[0m")
    

    To reset the color back to the default, you can use the escape sequence \033[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.

    <a id="h-3-terminal-colored-text-colorama-library"></a>

    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)
    

    <a id="h-4-termcolor-library-colored-terminal-output"></a>

    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"]))
    

    <a id="h-5-terminal-black-text-with-red-background"></a>

    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'))
    

    <a id="h-6-summary-and-conclusion"></a>

    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!

    <a id="h-related-articles"></a>

    Related Articles