Hello everyone, today we are going to create a short and fun project called Text Color Printer in Python.
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.
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
pip install colorama
Alright now let's go to the coding!
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...
We still need to import two separate functions from
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
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)
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: ")
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:
Let's try something with
print(Fore.RED + 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)
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)
Awesome we are done with this one!
You can find the complete source code of this project here -
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.