Print Color Text with Python
Tags: colorama, python, beginners, programming
Published: 2023-03-01 08:56:37

Hi everyone,

one of the most interesting things when I started using a computer, a Commodore 64, was always the ability to write colored text. It may sound childish, and indeed I was a child, but at the time the ability to display colors at my leisure was always something that filled me with joy. Of course, age and technological advancement have led me to consider different aspects of computing cool. But sometimes coming back as a child gives a little help to move forward. Therefore, in this post, the possibility of printing colored text in python will be discussed.

Introduction
On Linux, it is possible to print colored text using some `ANSI escape sequences [1], for example, if you open your terminal, and type:

`echo -e "\e[31mColored Text\e[0m"`

and you will see the following:

<p align="center">
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/agnlszb0iadnxindcimi.png">
</p>
<p align="center">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Write color text with Bash
</p>


The text is now Red. Entering in details, we can analyze the pattern:
echo -e "\e[COLORmSample Text\e[0m"
this string is formed by different parameters:

- -e: Enable interpretation of backslash escapes
- \e[: Start the color modifications
- COLORm: Represents the color code, plus m at the end
- \e[0m: End color modifications

Return to our example echo -e "\e[31mColored Text\e[0m", you can see that we use the code 31 to display the Red text. There are different complete articles on the Internet, I suggest you read [1] and [4]. Just to summarize, there are codes for color, code to have Bold, Underlined, and also Blinking Characters. Moreover, you can add more colors modification separated by `;`:


`echo -e "\e[COLOR1;COLOR2mText\e[0m"`

**From Bash to Python**
One fantastic thing is that you can reuse all the concepts that you learned until now also in python. Python supports color modifiers:


`print(f"\033[31mRed Text")`

In practice, the `\e` became `\033`. Ok, that's great, but writing the color modifications reduces the code's legibility. The best solution could be to use and high-level package which solves it.
This is the goal of the package colorama.

**Introduction to colorama**
Colorama is a package able to create the ANSI escape character sequences to colorize your output. Colorama is multi-platform it works under Linux, Mac, and Windows. The colorama's demo scripts print some colored text using ANSI sequences. The installation is simple, just type:


`pip install colorama`

If your environment is Windows, you have to init in the following way:

```
from colorama import just_fix_windows_console
just_fix_windows_console()
```

after that, you can use it.

Using Colorama
For starting to use Colorama, let's see the code for printing Red text:

```
import colorama
from colorama import Fore

print(Fore.RED + 'This text is red in color')
```

the output is:

<p align="center">
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ue9h9yozm8ytuqmppx7d.png">
</p>
<p align="center">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The first example with colorama
</p>

Continue Reading on [Syntax Error](https://medium.com/syntaxerrorpub/print-color-text-with-python-a0d10845596f)