0

I want to show the output messages in red color.

I have used the following code on Linux. It displays the messages in red colour.

def red(name):
    print ("\033[91m {}\033[00m" .format(name))

red('This should be displayed in red colour')

But when I use the same code on windows, it does not show in color.

Is there any common code which will work on both the OS ?

Where can I find the color codes ?

EDIT:

On windows command prompt, message was displayed like [91m This should be displayed in red colour[00m

Community
  • 1
  • 1
Dipankar Nalui
  • 1,121
  • 5
  • 18
  • 33
  • 1
    This depends more on the terminal emulator than the operating system, if the terminal does not support ansi escape codes it won't render as expected – Minn Nov 29 '18 at 10:14
  • Check out [PDCurses](https://github.com/wmcbrine/PDCurses#ports) which has been ported to many OSs. – martineau Nov 29 '18 at 10:43
  • You are jumping to the wrong conclusion here: "(1) My program displays red on Linux. (2) It does not show in red on Windows. (Which begs the question: *what color do you get, then?*). (3) Therefore, there must be another code to show red text on Windows. So, (4) what code works at all OSes?" At (3) is where you went wrong. You should be asking about (2). – Jongware Nov 29 '18 at 10:49
  • Duplicate of [Print in terminal with colors?](https://stackoverflow.com/q/287871/2564301). – Jongware Nov 29 '18 at 10:50
  • @usr2564301 On Linux Terminal it was showing on red color. But on Windows command prompt it does not show color. On windows message was displayed like `[91m This should be displayed in red colour[00m` – Dipankar Nalui Nov 29 '18 at 11:35

1 Answers1

2

You can use the termcolor module for this.

from termcolor import colored

print (colored('hello', 'red'), colored('world', 'green')) #Will print hello in red, world in green..

This works in most IDE's. If you are looking to re-color terminal output, you need to make the ANSI colors used in termcolor work with the windows terminal. To do this, you'll need to also import/init colorama.

from termcolor import colored
import colorama
colorama.init()
print(colored('hello','red'), colored('world', 'green'))
Psychotechnopath
  • 2,471
  • 5
  • 26
  • 47