How to Print Coloured Text in Python

When printing the output of a Python program to the terminal it may make it easier to read if we change the color of certain parts of the string. We can approach this in a number of different ways and use packages to make the process easier, which is what we will be learning in this tutorial.

 

Using ANSI Escape Sequences

The easiest way to print colored text from a Python program is to use ANSI escape sequences. To do this we will create a class containing properties for applying different colors to text. Let's create a file called colors.py containing a colors class, import that into the program then use it on an f string.

 

colors.py
class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[90m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
from colors import bcolors
return f'Successfully downloaded {bcolors.OKGREEN}{file.title}{bcolors.ENDC} to {formt}'

 

The color of the text is changed between bcolors.OKGREEN and bcolors.ENDC:

 

Colored text in the terminal

 

As we can see each ANSI character escape sequence takes the form \033[XXXm. The part after the [ (square bracket) is the style of the text. Numbers 90-96 provide a range of different text colors. Here are what they are:

 

NumberColor
90 grey
91 red
92green
93yellow
94 blue
95pink
96turquoise

 

Personally, I like this approach because you can define any font effect in the class then use it in your program.

 

Changing the Background Color of Text

To change the background color of text we can use the ANSI escape sequence character numbers 100-107. Let's expand the bcolors class we just created to include background colors.

 

colors.py
class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
    # Background colors:
    GREYBG = '\033[100m'
    REDBG = '\033[101m'
    GREENBG = '\033[102m'
    YELLOWBG = '\033[103m'
    BLUEBG = '\033[104m'
    PINKBG = '\033[105m'
    CYANBG = '\033[106m'
from colors import bcolors
return f'Successfully downloaded {bcolors.GREENBG}{file.title}{bcolors.ENDC} to {formt}'

 

Text background color

 

Here are all the available background colors and their corresponding ANSI number:

 

NumberColor
100 grey
101 red
102green
103yellow
104 blue
105pink
106turquoise

 

All the Available Font Effects

For reference here are all the available font effect codes to change the style of text in the terminal:

 

Number(s)Effect
0Reset / Normal
1Bold or increased intensity
2Faint (decreased intensity)
3Italic
4Underline
5Slow Blink
6Rapid Blink
7[[reverse video]]
8Conceal
9Crossed-out
10Primary(default) font
11–19Alternate font
20Fraktur
21Bold off or Double Underline
22Normal color or intensity
23Not italic, not Fraktur
24Underline off
25Blink off
27Inverse off
28Reveal
29Not crossed out
30–37Set foreground color
38Set foreground color
39Default foreground color
40–47Set background color
48Set background color
49Default background color
51Framed
52Encircled
53Overlined
54Not framed or encircled
55Not overlined
60ideogram underline
61ideogram double underline
62ideogram overline
63ideogram double overline
64ideogram stress marking
65ideogram attributes off
90–97Set bright foreground color
100–107Set bright background color

 

The colorama Package

If you would rather use a package to change the print colors in Python, install the colorama package. To install it type the following into your terminal:

 

pip install colorama

 

Then import the foreground, background and style classes into your program:

 

from colorama import Fore, Back, Style

 

To change some text to red use the Fore.RED and to set the rest of the text back to normal use Style.RESET_ALL

 

return f'Successfully downloaded {Fore.RED}{file.title}{Style.RESET_ALL} to {formt}'

 

red terminal text

 

Here are all the formatters available in colorama:

 

  • Fore - BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
  • Back - BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
  • Style - DIM, NORMAL, BRIGHT, RESET_ALL

 

Conclusion

You now know how to style the color text for printing in Python.

color text terminal print