How to build a Color Text Printer in Python
Tags: python, codenewbie, beginners, tutorial
Published: 2021-02-15 18:03:11

Hello everyone, today we are going to create a short and fun project called **Text Color Printer in Python**.

## What is a Text Color Printer?

We generally user Terminal to execute our programs and every time some output is printed. The font of the text printed on the console is pretty dull and most of the time it's just some white text on the black console. 

However, it is possible to alter the colours of the output text and the background console. Today we are going to do just that using a cool module.

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

## Project Setup

So first things first! We need to install the required module onto our system. To do that we will use the **`pip install`** command. The module we are going to install is called **`colorama`.**

```bash
pip install colorama
```

Alright now let's go to the coding!

## Let's Code

The first thing we are going to do is, of course, import the module we just installed into our project. Let's do it quickly...

```python
import colorama
```

We still need to import two separate functions from **`colorama`.**

**Those functions are:**

- **`Back` -** This function will be used to change the background colour of the text.
- **`Fore` -** This function will be used to change the colour of the text itself.

So now, let's import them...

```python
from colorama import Back, Fore
```

Now, this next step is optional but highly recommended...

We are going to make use of a function from **`colorama`** module which will restrict the colouring of the output only till the execution of it. Once the execution is done, the colouring will stop.

```python
colorama.init(autoreset = True)
```

**`colorama.init(autoreset = True)`** will reset the color values to default once the execution is over.

Now let's ask the user for some text which we will then print on the console.

```python
text = input("Enter a pharse or sentence: ")
```

Awesome! Now let's get crazy and start printing coloured text!

**One thing to note here is that we have limited colour options, which are as follows:**

- **`BLACK`**
- **`RED`**
- **`GREEN`**
- **`YELLOW`**
- **`BLUE`**
- **`MAGENTA`**
- **`CYAN`**
- **`WHITE`**

Let's try something with **`RED`!**

```python
print(Fore.RED + text)
```

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

This is what we get, text in Red colour!

Now, how about using **`Back`** instead of **`Fore`**? Let's try it out!

```python
print(Back.RED + text)
```

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

Here we are getting Red in the background.

Now let's mix and match everything and see how it looks!

```python
print(Fore.BLACK + Back.WHITE  + text)
print(Fore.RED + Back.CYAN  + text)
print(Fore.GREEN + Back.MAGENTA  + text)
print(Fore.YELLOW + Back.BLUE  + text)
print(Fore.BLUE + Back.YELLOW  + text)
print(Fore.MAGENTA + Back.GREEN  + text)
print(Fore.CYAN + Back.RED  + text)
print(Fore.WHITE + Back.BLACK  + text)
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/a3p33purzm7su183cr67.png)
Here we go! Looks cool right!


Awesome we are done with this one!

## Source Code

You can find the complete source code of this project here -

[mindninjaX/Python-Projects-for-Beginners](https://github.com/mindninjaX/Python-Projects-for-Beginners/tree/master/Color%20Text%20Printer)

## Support

Thank you so much for reading! I hope you found this beginner project useful. 

If you like my work please consider [Buying me a Coffee](https://buymeacoff.ee/mindninjaX) so that I can bring more projects, more articles for you.

![https://dev-to-uploads.s3.amazonaws.com/i/5irx7eny4412etlwnc64.png](https://dev-to-uploads.s3.amazonaws.com/i/5irx7eny4412etlwnc64.png)

Also if you have any questions or doubts feel free to contact me on [Twitter](https://twitter.com/mindninjaX), [LinkedIn](https://www.linkedin.com/in/mindninjax/) & [GitHub](https://github.com/mindninjaX). Or you can also post a comment/discussion & I will try my best to help you :D