2

If I had this code:

print("Hello World")

How would I change its colour to, say, green? I've tried ANSI escape codes and they don't work. (Python Shell)

  • Possible duplicate of [How to print colored text in terminal in Python?](https://stackoverflow.com/questions/287871/how-to-print-colored-text-in-terminal-in-python) – Nazim Kerimbekov May 01 '19 at 07:15

3 Answers3

2

ANSI escape codes work for me:

ANSI_GREEN = '\033[92m'
ANSI_NORMAL = '\033[0m'
print ANSI_GREEN + "Hello world" + ANSI_NORMAL

This does, of course, depend on what kind of terminal is displaying the output.

slim
  • 40,215
  • 13
  • 94
  • 127
1

I presume you're using a windows terminal?

From Wikipedia:

Although hardware text terminals have become increasingly rare in the 21st century, the relevance of the ANSI standard persists because most terminal emulators interpret at least some of the ANSI escape sequences in the output text. One notable exception was the Win32 console component of Microsoft Windows before Windows 10 update TH2.

support for escape codes is dependent on the terminal you're using. try cygwin terminal or something else.

Aaron
  • 10,133
  • 1
  • 24
  • 40
  • Good Lord. That's astonishing. In my answer, the exceptions I had in mind were hardware green-screens, or piping to some other program. – slim Feb 17 '17 at 17:05
0

You can use the package termcolor

LucG
  • 1,238
  • 13
  • 25