Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing colors in python terminal [duplicate]

Tags:

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.

like image 761
Ethan Bierlein Avatar asked Apr 05 '14 20:04

Ethan Bierlein


People also ask

How do you color text in Python?

Method 1: Using ANSI ESCAPE CODE To add color and style to text, you should create a class called ANSI, and inside this class, declare the configurations about the text and color with code ANSI. Functions Used: background: allows background formatting. Accepts ANSI codes between 40 and 47, 100 and 107.


1 Answers

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) 
like image 107
Liam McInroy Avatar answered Sep 19 '22 07:09

Liam McInroy