Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print to console in color? [duplicate]

Tags:

python

ubuntu

How to print in color using python print. For example

print('This should be red')
print('This should be green')

Now everything is white text on black background. I use ubuntu, if it helps.

like image 401
user3654650 Avatar asked Dec 03 '14 06:12

user3654650


People also ask

How do I print text with different colors?

Use the colored module. import colored color = colored.fg (196) #orange print (color + "This text is orange") Below is a handy function I find useful. It will print the text you provide in the desired foreground and background colors you specify using standard RGB tuples so you do not have to remember ANSI codes.

How do you print color in Python terminal?

‘termcolor’ module: termcolor is a python module for ANSII Color formatting for output in the terminal. print_red_on_cyan ('Hello, World!') print_red_on_cyan ('Hello, Universe!') The most common way to print colored text is by printing ANSI escape sequences directly.

How do I print orange text in Python?

On windows you could also need the colorama package (see the questions that this one duplicates). Use the colored module. import colored color = colored.fg (196) #orange print (color + "This text is orange") Below is a handy function I find useful.

How to print colored text using ANSI escape codes?

Using ANSI Escape Codes. The most common way to print colored text is by printing ANSI escape sequences directly. This can be delivered in different formats such as: Build Functions to call : We can build functions to call particular color named functions to execute the relevant ANSI Escape Sequence.


1 Answers

Define color like this:

W  = '\033[0m'  # white (normal)
R  = '\033[31m' # red
G  = '\033[32m' # green
O  = '\033[33m' # orange
B  = '\033[34m' # blue
P  = '\033[35m' # purple

print(R+"hello how are you"+W)

Demo: Demo

see all color codes here:Color Codes

like image 154
Hackaholic Avatar answered Sep 21 '22 06:09

Hackaholic