DEV Community

Cover image for Colored text in terminal using Python
Muhimen
Muhimen

Posted on

Colored text in terminal using Python

The terminal, a love-hate relationship with the developers. Whether you love to use the terminal(me) or you hate it, you (probably)have spent a significant amount of time looking at the plain black and white text of the terminal(not applicable for the new windows terminal though 🤗). From time to time, the terminal has evolved to cope up with the needs of a developer.

From this

Alt Text

To this 😎

Alt Text

And showing colored text in the terminal is probably a useful(if not cool) feature ever added. Today, I'll demonstrate how you can output colored text in the terminal using Python with the help of Colorama

Installation

We just need only one package. And it is colorama. To install colorama, just simply turn on your command prompt and write the following command.



pip install colorama


Enter fullscreen mode Exit fullscreen mode

And we are good to go.

Changing text color

In colorama, the font color or the text color is referred to as Fore(stands for foreground). To change the font color, start by importing the module.



import colorama
colorama.init()


Enter fullscreen mode Exit fullscreen mode

(For Windows only, you need to initialize colorama to output colors in the terminal, not ASCII escape sequence)
Then, in the print statement, mention the color you want to use. Just like this.



print("This is a normal text")
print(colorama.Fore.RED, "This is a red color text")


Enter fullscreen mode Exit fullscreen mode

You will see an output like this.

Alt Text

However, if you don't initialize colorama with this colorama.init() your output will look like this.

Alt Text

Other than red, you can choose any color from this list.

  • BLACK (default)
  • BLUE
  • CYAN
  • GREEN
  • LIGHTBLACK_EX
  • LIGHTBLUE_EX
  • LIGHTCYAN_EX
  • LIGHTGREEN_EX
  • LIGHTMAGENTA_EX

  • LIGHTRED_EX

  • LIGHTYELLOW_EX

  • MAGENTA

  • WHITE

  • YELLOW

Point to be noted: You must type the color name all in upper case

Changing background-color

In colorama, background color is referred to as Back(stands for background). So, if you want to change the background color of any text, replace Fore with Back. Here is the implementation of the above code but with the background color.



import colorama
colorama.init()

print("Text with normal background")
print(colorama.Back.CYAN, "Text with cyan background")


Enter fullscreen mode Exit fullscreen mode

Let's run the code! And here is the possible outcome

Alt Text

And the available color option is the same as the foreground.

Changing text style

You have three options to choose from.

  • Bright
  • Normal
  • Dim

Let's try them all!



import colorama
colorama.init()

print(colorama.Style.NORMAL, "I am a normal text")
print(colorama.Style.BRIGHT, "I am a bright text")
print(colorama.Style.DIM, "I am a dim text")


Enter fullscreen mode Exit fullscreen mode

Alt Text

It's a bit tough to see them but there is a subtle difference.

Reseting the color

Once you apply color in the terminal, whether you want it or not, it will keep repeating until some other color is applied. Something like this.

Alt Text

To get rid this of functionality, you have three options.

  • colorama.Style.RESET_ALL => Reset's both foreground & background color
  • colorama.Fore.RESET => Reset's foreground color
  • colorama.Back.RESET => Reset's background color

Example

Here is a simple Python script that will look for a certain element in a list.



from colorama import Style, Fore, Back
from tqdm import trange
colorama.init()

ls = [1,2,3,4,5,6,7,8,9,10,12,14,15]
search1 = 15
search2 = 11
found1 = False
found2 = False

print(Fore.CYAN)
for i in trange(len(ls)):
    if ls[i] == search1:
        found1 = True
        break
    if ls[i] == search2:
        found2 = True
        break
print(Style.RESET_ALL)
if found1:
    print(Fore.GREEN, 'We found the number you were looking for', Style.RESET_ALL)
else:
    print(Fore.RED, 'Sorry, the number you are looking for is not in the list',Style.RESET_ALL)

if found2:
    print(Fore.GREEN, 'We found the number you were looking for', Style.RESET_ALL)
else:
    print(Fore.RED, 'Sorry, the number you are looking for is not in the list', Style.RESET_ALL)


Enter fullscreen mode Exit fullscreen mode

Here is the output.

Alt Text

Nothing fancy and doesn't speed up your code. But it still looks cool, right? 😉

I hope you enjoyed it. If you want to learn more about colorama, I'll highly encourage you to read the official documentation.

Until next time, happy coding for you. 😊

Top comments (0)