-1

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.

rnso
  • 23,686
  • 25
  • 112
  • 234
  • `\e` is not the correct escape sequence to enable colours. Did you read the answers properly? – Martijn Pieters Nov 05 '17 at 12:15
  • I am referring to answer by @IgorSarcevic and using above code from a script. – rnso Nov 05 '17 at 12:16
  • That *single answer* that used `\e` is wrong. `\e` is an escape sequence in a different shell, not in Python. I've left a comment on it stating how it is wrong; the answer doesn't even work in bash. – Martijn Pieters Nov 05 '17 at 12:16
  • Your question links to the whole question, not to a specific answer, I had to search for it (it only appears on page 2 when sorting by votes). – Martijn Pieters Nov 05 '17 at 12:18

1 Answers1

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343