How to print colored Python output on the terminal?

Posted in /  

How to print colored Python output on the terminal?
vinaykhatri

Vinay Khatri
Last updated on December 24, 2024

    At the beginning stage, we generally built console-based Python projects where we simply printed the output on the terminal with the black background and white text. Although on Linux and mac, when we print the output on the console, we have an option to change the color theme of the console and text. On Windows, we do not get that feature. There, we have to make our work done with the traditional command prompt.

    However, it would be cool. Instead of changing the color of the terminal, we could use Python programming to print the colorful output using the code.

    In this Python tutorial, you will learn how to print and give color to your output text using the popular Python colorama library.

    How to print colored Python output on the terminal?

    colorama library

    colorama is an open-source Python library that is used to print color terminal text and cursor position on windows and other operating systems. Install colorama for your Python environment and run the following pip command on your terminal or command prompt.

    pip install colorama

    Print Colorful Python text on the terminal

    To print the colorful text on the terminal, we can use the colorama module Fore class, along with the print statement.

    Example

    from colorama import init   #for windows 
    init()                      #for windows
    from colorama import Fore
    print(Fore.RED+ "This text will be printed red")
    print(Fore.GREEN+ "This text will be printed green")
    print(Fore.YELLOW+ "This text will be printed yellow")

    Output

    If you are on windows, it's very important to write from colorama import init and init() statement before you use any Colorama method. These two statements will have no effect on other operating systems, but on windows, it will filter ANSI escape sequences out of any text sent to stdout or stderr , and replace them with equivalent Win32 calls.

    Colorful Python output background on the terminal

    To make the background color, we can use the colorama Back class, with the print statement.

    Example

    from colorama import init   #for windows 
    init()                      #for windows
    from colorama import Back
    print(Back.RED+ "Background will be red")
    print(Back.GREEN+ " Background will be green")
    print(Back.YELLOW+ "Background will be yellow")

    Output

    Dim or Bright the output

    We can also change the brightness of the output text using the Colorama Style class.

    Example

    from colorama import init   #for windows 
    init()                      #for windows
    from colorama import Style
    print(Style.DIM+ "Dim text")
    print(Style.BRIGHT+ "Bright Text")

    Output

    Conclusion

    In this Python tutorial, you learned how to print colorful text output in Python. In other operating systems, we can simply use the ANSI escape character sequence to print the colorful text on the terminal there. We do not have to use Colorama. But in the window operating system, we need Colorama to wrap stdout , strip ANSI sequences, and convert them into the appropriate win32 calls to modify the state of the terminal.

    People are also reading:

    Leave a Comment on this Post

    0 Comments