[Python] Color 'print' in Python

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

  1. Stack

    Stack Membro Participativo

    Been going thru this: How do I print colored text to the terminal?. My issue is little different than those solutions (or I just can't find it). I need to print two variables in different colors in same print statement.

    For example print("{0} {1}".format(test1, test2)) Should print 'one' in RED and 'two' in BLUE.

    Below works for single line. But how do I combine them.

    os.system("")
    class style():
    RED = '\033[31m'
    GREEN = '\033[32m'
    BLUE = '\033[34m'
    RESET = '\033[0m'

    test1 = "ONE"
    test2 = "TWO"

    print(style.RED + "{0}".format(test1) + style.RESET)
    print(style.GREEN + "{0}".format(test2) + style.RESET)

    Continue reading...

Compartilhe esta Página