[Python] How do I print colored output to the terminal in Python?

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    Is there a Python equivalent of Perl's

    print color 'red';
    print <something>;
    print color 'reset';


    available?

    I am using this approach:

    "\x1b[1;%dm" % (<color code>) + "ERROR: log file does not exist" + "\x1b[0m"


    what I want is I should be able to set color for all print messages like,

    print color 'red'
    function_print_something(<some message>)
    print color 'reset'


    Here 'function_print_something' is my python function which will print some formatted log messages on to the screen.

    Continue reading...

Compartilhe esta Página