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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With