Colored text in terminal using Python
Tags: python, tutorial, beginners
Published: 2020-06-11 10:02:01

**The terminal**, a love-hate relationship with the developers. Whether you love to use the terminal(me) or you hate it, you (probably)have spent a significant amount of time looking at the plain black and white text of the terminal(not applicable for the new windows terminal though 🤗). From time to time, the terminal has evolved to cope up with the needs of a developer.

From this

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wngjjxvwr3dqgahidm1d.png)

To this 😎

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o4h8y28i5yepwso1y9iw.png)

And showing colored text in the terminal is probably a useful(if not cool) feature ever added. Today, I'll demonstrate how you can output colored text in the terminal using **Python** with the help of <mark>Colorama</mark>

## Installation
We just need only one package. And it is **colorama**. To install colorama, just simply turn on your command prompt and write the following command.
```bash
pip install colorama
```
And we are good to go.

## Changing text color
In colorama, the font color or the text color is referred to as **Fore**(stands for foreground). To change the font color, start by importing the module.

```python
import colorama
colorama.init()
```
(For Windows only, you need to initialize colorama to output colors in the terminal, not ASCII escape sequence)
Then, in the print statement, mention the color you want to use. Just like this. 

```python
print("This is a normal text")
print(colorama.Fore.RED, "This is a red color text")
```
You will see an output like this.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e0bx9ey8xywfferkrcsk.png)

However, if you don't initialize colorama with this `colorama.init()` your output will look like this.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mmhecaltiv5vj8j05it1.png)

Other than red, you can choose any color from this list.
- BLACK (default)
- BLUE
- CYAN
- GREEN
- LIGHTBLACK_EX
- LIGHTBLUE_EX
- LIGHTCYAN_EX
- LIGHTGREEN_EX
- LIGHTMAGENTA_EX

- LIGHTRED_EX
- LIGHTYELLOW_EX
- MAGENTA
- WHITE
- YELLOW

<mark>Point to be noted:</mark> You must type the color name all in **upper case**

## Changing background-color

In colorama, background color is referred to as **Back**(stands for background). So, if you want to change the background color of any text, replace `Fore` with `Back`. Here is the implementation of the above code but with the background color.

```python
import colorama
colorama.init()

print("Text with normal background")
print(colorama.Back.CYAN, "Text with cyan background")
```


Let's run the code! And here is the possible outcome

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o5q8w5ly9jxw3urepv3p.png)

And the available color option is the same as the foreground.

## Changing text style
You have three options to choose from.
- Bright 
- Normal
- Dim

Let's try them all!

```python
import colorama
colorama.init()

print(colorama.Style.NORMAL, "I am a normal text")
print(colorama.Style.BRIGHT, "I am a bright text")
print(colorama.Style.DIM, "I am a dim text")
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gyd0yo85niiwzsp10svd.png)

It's a bit tough to see them but there is a subtle difference.

## Reseting the color
Once you apply color in the terminal, whether you want it or not, it will keep repeating until some other color is applied. Something like this.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gluisd084hevq34e2xwn.png)

To get rid this of functionality, you have three options.
- colorama.Style.RESET_ALL => Reset's both foreground & background color
- colorama.Fore.RESET => Reset's foreground color
- colorama.Back.RESET => Reset's background color

## Example
Here is a simple Python script that will look for a certain element in a list. 

```python
from colorama import Style, Fore, Back
from tqdm import trange
colorama.init()

ls = [1,2,3,4,5,6,7,8,9,10,12,14,15]
search1 = 15
search2 = 11
found1 = False
found2 = False

print(Fore.CYAN)
for i in trange(len(ls)):
    if ls[i] == search1:
        found1 = True
        break
    if ls[i] == search2:
        found2 = True
        break
print(Style.RESET_ALL)
if found1:
    print(Fore.GREEN, 'We found the number you were looking for', Style.RESET_ALL)
else:
    print(Fore.RED, 'Sorry, the number you are looking for is not in the list',Style.RESET_ALL)

if found2:
    print(Fore.GREEN, 'We found the number you were looking for', Style.RESET_ALL)
else:
    print(Fore.RED, 'Sorry, the number you are looking for is not in the list', Style.RESET_ALL)
```

Here is the output.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wmurzdglaxmhl9q28rc9.png)

Nothing fancy and doesn't speed up your code. But it still looks cool, right? 😉

I hope you enjoyed it. If you want to learn more about colorama, I'll highly encourage you to read the official [documentation](https://github.com/tartley/colorama).

Until next time, happy coding for you. 😊