Not able to print in color on terminal

旧城冷巷雨未停 提交于 2019-12-13 08:55:34

问题


As given in an answer on Print in terminal with colors using Python? , I am trying to print in color on console/terminal using following code:

RED = "\e[31m"
NORMAL = "\e[0m"

print("TESTING")
print(RED+"TESTING"+NORMAL)
print("TESTING")

However, it is not working and only giving following output:

TESTING
\e[31mTESTING\e[0m      # IN BLACK, THOUGH IT IS SHOWING COLOR HERE.
TESTING

Where is the problem and how can it be solved? I am using Python version 3.5.3 on Debian Stable Linux.


回答1:


You have found a wrong answer; \e is not a valid escape sequence in Python. In some echo implementations, \e is an escape sequence for the ASCII ESC character, but in Python you need to use a different notation.

The rest of the answers on that page use correct forms, either \x1b or \033.



来源:https://stackoverflow.com/questions/47121421/not-able-to-print-in-color-on-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!