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.
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
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
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
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:
BLACK
RED
GREEN
YELLOW
BLUE
MAGENTA
CYAN
WHITE
Let's try something with RED
!
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!
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.
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)