Search code examples
pythonwindowsescapingtextcolor

How to fix windows escape characters not working correctly in python?


I am currently trying to get python to show colored text in the windows terminal. Unfortunately, the escape sequence is not being interpreted correctly.

At first, I tried to use different libraries including "crayons", "termcolor" and "colorama". Unfortunately, these show similar problems to my code.

print(str('\033[31m' + 'this should be red'))

The output should be the text "this should be red", but in red instead of the standard white. The actual output is "[31msome red text", in white and with a weird special character showcased as a box with a question mark in it at the start.


Solution

  • See comment by eryksun: colorama.init() modifies sys.stdout in such a way that the output is displyed correctly in color.