12

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.

user3654650
  • 5,283
  • 10
  • 27
  • 28

5 Answers5

44

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

Hackaholic
  • 19,069
  • 5
  • 54
  • 72
4

Use the colored module.

import colored
color = colored.fg(196) #orange
print(color + "This text is orange")

https://pypi.org/project/colored/

Coll y
  • 83
  • 1
  • 3
  • 11
4

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. To find the RGB values you may want to use you can use the color picker at https://www.w3schools.com/colors/colors_picker.asp.

def print_in_color(txt_msg,fore_tupple,back_tupple,):
    #prints the text_msg in the foreground color specified by fore_tupple with the background specified by back_tupple 
    #text_msg is the text, fore_tupple is foregroud color tupple (r,g,b), back_tupple is background tupple (r,g,b)
    rf,gf,bf=fore_tupple
    rb,gb,bb=back_tupple
    msg='{0}' + txt_msg
    mat='\33[38;2;' + str(rf) +';' + str(gf) + ';' + str(bf) + ';48;2;' + str(rb) + ';' +str(gb) + ';' + str(bb) +'m' 
    print(msg .format(mat))
    print('\33[0m') # returns default print color to back to black

# example of use using a message with variables
fore_color='cyan'
back_color='dark green'
msg='foreground color is {0} and the background color is {1}'.format(fore_color, back_color)
print_in_color(msg, (0,255,255),(0,127,127))
Gerry P
  • 7,662
  • 3
  • 10
  • 20
  • should be "rf,gf,bf=fore_tupple" not "rf,bf,gf=fore_tupple" – ZDL-so May 03 '20 at 05:38
  • @GerryP This is absolutely wonderful, thank you. – Guimoute Apr 01 '22 at 14:43
  • Great method Using a single `print` to print the message and return to the default black print color allows to avoid blank lines: `print(msg .format(mat) + '\33[0m' )` – Max Sep 05 '22 at 14:10
1

Using a module such as colorconsole is easier:

pip install colorconsole

Then e.g.

from colorconsole import terminal

screen = terminal.get_terminal(conEmu=False)

screen.cprint(4, 0, "This is red\n")
screen.cprint(10, 0, "This is light green\n")
screen.cprint(0, 11, "This is black on light cyan\n")

screen.reset_colors()

It also supports 256/24 bit colours if available.

spiralx
  • 1,035
  • 7
  • 16
0

Use this Function here: It has the colors: red, blue, green

colors = {'red':'\033[31m', 'blue':'\033[34m', 'green':'\033[32m'}

def colorprint(string, text_color = 'default', bold = False, underline = False):
    if underline == True:
            string = '\033[4m' + string
    if bold == True:
            string = '\033[1m' + string
    if text_color == 'default' or text_color in colors:
            for color in colors:
                    if text_color == color:
                            string = colors[color] + string
    else:
            raise ValueError ("Colors not in:", colors.keys())

    print(string + '\033[0m')