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

前端 未结 7 1042
日久生厌
日久生厌 2020-12-04 17:55

Is there any python equivalent for perl\'s

print color \'red\';
print ;
print color \'reset\';

available in python?

<
相关标签:
7条回答
  • 2020-12-04 18:23

    You can try this with python 3:

    from termcolor import colored
    print(colored('Hello, World!', 'green', 'on_red'))
    

    If you are using windows operating system, the above code may not work for you. Then you can try this code:

    from colorama import init
    from termcolor import colored
    
    # use Colorama to make Termcolor work on Windows too
    init()
    
    # then use Termcolor for all colored text output
    print(colored('Hello, World!', 'green', 'on_red'))
    

    Hope that helps.

    0 讨论(0)
提交回复
热议问题