Python Projects for Beginners (15 Part Series)
1 How to build an Acronym Generator in Python
2 How to build an Alarm Clock in Python
… 11 more parts…
3 How to build an Email Slicer using Python
4 How to build a Random Story Generator using Python
5 How to build a Password Generator using Python
6 How to build Rock Paper Scissors Game in Python
7 How to build a Dice Roller in Python
8 How to build a QR Code Generator in Python
9 How to build a Quiz Game in Python
10 How to build a Color Text Printer in Python
11 How to build a BMI Calculator in Python
12 How to build an Fahrenheit to Celsius Converter in Python
13 How to build an Echo Chatbot in Python
14 How to build a Guess the Number Game in Python
15 How to build a Madlib Game 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.
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 <span>install </span>coloramapip <span>install </span>coloramapip 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…
<span>import</span> <span>colorama</span><span>import</span> <span>colorama</span>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…
<span>from</span> <span>colorama</span> <span>import</span> <span>Back</span><span>,</span> <span>Fore</span><span>from</span> <span>colorama</span> <span>import</span> <span>Back</span><span>,</span> <span>Fore</span>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.
<span>colorama</span><span>.</span><span>init</span><span>(</span><span>autoreset</span> <span>=</span> <span>True</span><span>)</span><span>colorama</span><span>.</span><span>init</span><span>(</span><span>autoreset</span> <span>=</span> <span>True</span><span>)</span>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.
<span>text</span> <span>=</span> <span>input</span><span>(</span><span>"</span><span>Enter a pharse or sentence: </span><span>"</span><span>)</span><span>text</span> <span>=</span> <span>input</span><span>(</span><span>"</span><span>Enter a pharse or sentence: </span><span>"</span><span>)</span>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
!
<span>print</span><span>(</span><span>Fore</span><span>.</span><span>RED</span> <span>+</span> <span>text</span><span>)</span><span>print</span><span>(</span><span>Fore</span><span>.</span><span>RED</span> <span>+</span> <span>text</span><span>)</span>print(Fore.RED + text)
Enter fullscreen mode Exit fullscreen mode
This is what we get, text in Red colour!
Now, how about using Back
instead of Fore
? Let’s try it out!
<span>print</span><span>(</span><span>Back</span><span>.</span><span>RED</span> <span>+</span> <span>text</span><span>)</span><span>print</span><span>(</span><span>Back</span><span>.</span><span>RED</span> <span>+</span> <span>text</span><span>)</span>print(Back.RED + text)
Enter fullscreen mode Exit fullscreen mode
Here we are getting Red in the background.
Now let’s mix and match everything and see how it looks!
<span>print</span><span>(</span><span>Fore</span><span>.</span><span>BLACK</span> <span>+</span> <span>Back</span><span>.</span><span>WHITE</span> <span>+</span> <span>text</span><span>)</span><span>print</span><span>(</span><span>Fore</span><span>.</span><span>RED</span> <span>+</span> <span>Back</span><span>.</span><span>CYAN</span> <span>+</span> <span>text</span><span>)</span><span>print</span><span>(</span><span>Fore</span><span>.</span><span>GREEN</span> <span>+</span> <span>Back</span><span>.</span><span>MAGENTA</span> <span>+</span> <span>text</span><span>)</span><span>print</span><span>(</span><span>Fore</span><span>.</span><span>YELLOW</span> <span>+</span> <span>Back</span><span>.</span><span>BLUE</span> <span>+</span> <span>text</span><span>)</span><span>print</span><span>(</span><span>Fore</span><span>.</span><span>BLUE</span> <span>+</span> <span>Back</span><span>.</span><span>YELLOW</span> <span>+</span> <span>text</span><span>)</span><span>print</span><span>(</span><span>Fore</span><span>.</span><span>MAGENTA</span> <span>+</span> <span>Back</span><span>.</span><span>GREEN</span> <span>+</span> <span>text</span><span>)</span><span>print</span><span>(</span><span>Fore</span><span>.</span><span>CYAN</span> <span>+</span> <span>Back</span><span>.</span><span>RED</span> <span>+</span> <span>text</span><span>)</span><span>print</span><span>(</span><span>Fore</span><span>.</span><span>WHITE</span> <span>+</span> <span>Back</span><span>.</span><span>BLACK</span> <span>+</span> <span>text</span><span>)</span><span>print</span><span>(</span><span>Fore</span><span>.</span><span>BLACK</span> <span>+</span> <span>Back</span><span>.</span><span>WHITE</span> <span>+</span> <span>text</span><span>)</span> <span>print</span><span>(</span><span>Fore</span><span>.</span><span>RED</span> <span>+</span> <span>Back</span><span>.</span><span>CYAN</span> <span>+</span> <span>text</span><span>)</span> <span>print</span><span>(</span><span>Fore</span><span>.</span><span>GREEN</span> <span>+</span> <span>Back</span><span>.</span><span>MAGENTA</span> <span>+</span> <span>text</span><span>)</span> <span>print</span><span>(</span><span>Fore</span><span>.</span><span>YELLOW</span> <span>+</span> <span>Back</span><span>.</span><span>BLUE</span> <span>+</span> <span>text</span><span>)</span> <span>print</span><span>(</span><span>Fore</span><span>.</span><span>BLUE</span> <span>+</span> <span>Back</span><span>.</span><span>YELLOW</span> <span>+</span> <span>text</span><span>)</span> <span>print</span><span>(</span><span>Fore</span><span>.</span><span>MAGENTA</span> <span>+</span> <span>Back</span><span>.</span><span>GREEN</span> <span>+</span> <span>text</span><span>)</span> <span>print</span><span>(</span><span>Fore</span><span>.</span><span>CYAN</span> <span>+</span> <span>Back</span><span>.</span><span>RED</span> <span>+</span> <span>text</span><span>)</span> <span>print</span><span>(</span><span>Fore</span><span>.</span><span>WHITE</span> <span>+</span> <span>Back</span><span>.</span><span>BLACK</span> <span>+</span> <span>text</span><span>)</span>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
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 😀
Python Projects for Beginners (15 Part Series)
1 How to build an Acronym Generator in Python
2 How to build an Alarm Clock in Python
… 11 more parts…
3 How to build an Email Slicer using Python
4 How to build a Random Story Generator using Python
5 How to build a Password Generator using Python
6 How to build Rock Paper Scissors Game in Python
7 How to build a Dice Roller in Python
8 How to build a QR Code Generator in Python
9 How to build a Quiz Game in Python
10 How to build a Color Text Printer in Python
11 How to build a BMI Calculator in Python
12 How to build an Fahrenheit to Celsius Converter in Python
13 How to build an Echo Chatbot in Python
14 How to build a Guess the Number Game in Python
15 How to build a Madlib Game in Python
暂无评论内容