问题
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