How to print colored text in Python?

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

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

30条回答
  •  终归单人心
    2020-11-21 05:12

    You want to learn about ANSI escape sequences. Here's a brief example:

    CSI="\x1B["
    print(CSI+"31;40m" + "Colored Text" + CSI + "0m")
    

    For more info see http://en.wikipedia.org/wiki/ANSI_escape_code

    For a block character, try a unicode character like \u2588:

    print(u"\u2588")
    

    Putting it all together:

    print(CSI+"31;40m" + u"\u2588" + CSI + "0m")
    

提交回复
热议问题