DEV Community

Cover image for How to build a Color Text Printer in Python
Rishabh Singh ⚡
Rishabh Singh ⚡

Posted on

How to build a Color Text Printer in Python

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

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.

pip install colorama
Enter fullscreen mode Exit fullscreen mode

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...

import colorama
Enter fullscreen mode Exit fullscreen mode

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...

from colorama import Back, Fore
Enter fullscreen mode Exit fullscreen mode

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.

colorama.init(autoreset = True)
Enter fullscreen mode Exit fullscreen mode

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.

text = input("Enter a pharse or sentence: ")
Enter fullscreen mode Exit fullscreen mode

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!

print(Fore.RED + text)
Enter fullscreen mode Exit fullscreen mode

Alt Text

This is what we get, text in Red colour!

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

print(Back.RED + text)
Enter fullscreen mode Exit fullscreen mode

Alt Text

Here we are getting Red in the background.

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

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)
Enter fullscreen mode Exit fullscreen mode

Alt Text
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

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 so that I can bring more projects, more articles for you.

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, LinkedIn & GitHub. Or you can also post a comment/discussion & I will try my best to help you :D

Top comments (0)