0

I have this python script in which I run in terminal. Currently I am trying to incorporate colors, eg. if it is 'error', the text will be red, if it is 'working', the text will be green.

I have tried to refer to this post that I have found but when I tried to run it in my terminal using the following code:

print('\x1b[0;31;40m' + 'Error!' + '\x1b[0m')

I am getting red text with a greyish background instead of red text with black background. My terminal uses Python 2.6.2.

My question here is: 1. Is there any way to get rid of this 'greyish' background? 2. Instead of setting colors to the background, is there an 'invisible' option? Eg. if I run the command, the printed text background will conform to the terminal background?

dissidia
  • 1,531
  • 3
  • 23
  • 53
  • Would trying a package like https://pypi.python.org/pypi/colorama be helpful for you? – Brian Aug 01 '17 at 01:20
  • 1
    `print('\x1b[31m' + 'Error!' + '\x1b[0m')` will print in red on the default background, unless you're using a custom palette. Take a look at https://en.wikipedia.org/wiki/ANSI_escape_code#Colors – PM 2Ring Aug 01 '17 at 01:20

1 Answers1

2

On a similar note, you can define colors and text decorators you want to use in a class and use them during printing instead of explicitly stating them in every print:

class Colors:
    Green, Red, White = '\033[92m', '\033[91m', '\033[0m'
    Bold, Italics = '\033[1m', '\x1B[3m'

print(Colors.Green + Colors.Bold + "I'm bold and green!")
Arex
  • 52
  • 5