22

I'd like to make a program that prints colors in the python terminal but I don't know how. I've heard that you can use certain escape sequences to print text in color, but I'm not sure of this. How can I print a string in a specific color using the python terminal?

Side note: I run a version of Linux.

Ethan Bierlein
  • 3,353
  • 4
  • 28
  • 42

1 Answers1

54

Try the termcolor module.

from termcolor import colored

print colored('hello', 'red'), colored('world', 'green')

See Print in terminal with colors using Python?

Also, you could use ANSI codes:

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

    def disable(self):
        self.HEADER = ''
        self.OKBLUE = ''
        self.OKGREEN = ''
        self.WARNING = ''
        self.FAIL = ''
        self.ENDC = ''

print(bcolors.WARNING + "Warning" + bcolors.ENDC)
Community
  • 1
  • 1
Liam McInroy
  • 4,339
  • 5
  • 32
  • 53