How To Print Colored Text in Python
Changing the colour of certain parts of a string when printing the output of a Python programme to the terminal may make it easier to read. We can approach this in a variety of ways and use packages to make things easier, as we will see in this tutorial.
Using ANSI Escape Sequences
The most basic way to print coloured text from a Python programme is to use ANSI escape sequences. To accomplish this, we will create a class with properties for applying different colours to text. Let's create a file called colors.py that contains a colours class, import it into the programme, and then use it on a f string.
The colors.py would look like this:
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'
We can import the bcolors class from colors.py using the following statements.
from colors import bcolors
return f'Successfully downloaded {bcolors.OKGREEN}{file.title}{bcolors.ENDC} to {formt}'
Each ANSI character escape sequence, as we can see, takes the form 033[XXXm. The text style is indicated by the part following the [(square bracket). Numbers 90-96 offer a variety of text colours. Here are some examples:
Number | Color |
90 | grey |
91 | red |
92 | green |
93 | yellow |
94 | blue |
95 | pink |
96 | turquoises |
Changing the Background Color of Text
To change the background colour of text, use the ANSI escape sequence character numbers 100-107. Let's go ahead and add some background colours to the bcolors class we just created.
Colors.py should now look like this:
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'
Let us see how this will work.
return f'Successfully downloaded {bcolors.GREENBG}{file.title}{bcolors.ENDC} to {formt}'
Here are all the available background colors and their corresponding ANSI number:
Number | Color |
100 | gray |
101 | red |
102 | green |
103 | yellow |
104 | blue |
105 | pink |
106 | turquoise |
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 |
0 | Reset / Normal |
1 | Bold or increased intensity |
2 | Faint (decreased intensity) |
3 | Italic |
4 | Underline |
5 | Slow Blink |
6 | Rapid Blink |
7 | [[reverse video]] |
8 | Conceal |
9 | Crossed-out |
10 | Primary(default) font |
11–19 | Alternate font |
20 | Fraktur |
21 | Bold off or Double Underline |
22 | Normal color or intensity |
23 | Not italic, not Fraktur |
24 | Underline off |
25 | Blink off |
27 | Inverse off |
28 | Reveal |
29 | Not crossed out |
30–37 | Set foreground color |
38 | Set foreground color |
39 | Default foreground color |
40–47 | Set background color |
48 | Set background color |
49 | Default background color |
51 | Framed |
52 | Encircled |
53 | Overlined |
54 | Not framed or encircled |
55 | Not overlined |
60 | ideogram underline |
61 | ideogram double underline |
62 | ideogram overline |
63 | ideogram double overline |
64 | ideogram stress marking |
65 | ideogram attributes off |
90–97 | Set bright foreground color |
100–107 | Set bright background color |
The colorama Package
Install the colorama package if you'd rather use a package to change the print colours in Python. To install it, enter the following command into your terminal:
pip install colorama
Then, in your programme, import the foreground, background, and style classes:
from colorama import Fore, Back, Style
To make some text red, use Fore.RED, and to make the rest of the text normal, use Style. RESET ALL
return f'Successfully downloaded {Fore.RED}{file.title}{Style.RESET_ALL} to {formt}'
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
Now you can easily print colored text in the python terminal!
Keep Learning!