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.
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.
‘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.
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.
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.
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:
see all color codes here:Color Codes
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