How to print colored text in Python?

前端 未结 30 2978
谎友^
谎友^ 2020-11-21 04:41

How can I output colored text to the terminal in Python?

30条回答
  •  自闭症患者
    2020-11-21 05:12

    I'm surprised no one has mentioned the Python termcolor module. Usage is pretty simple:

    from termcolor import colored
    
    print colored('hello', 'red'), colored('world', 'green')
    

    Or in Python 3:

    print(colored('hello', 'red'), colored('world', 'green'))
    

    It may not be sophisticated enough, however, for game programming and the "colored blocks" that you want to do...

提交回复
热议问题