Color Codes In Python

In this article, we are going to cover the topic of color codes in python, but first let’s discuss a few basic concepts.

What are Color Codes?

Color codes are the codes that are understandable by the computer, suppose we want to write a text but in red or in a different color so for that, we can’t directly tell the computer to make it red we have to pass it some sort of code so the computer will understand and print the text in that specific color.

Similarly, in python, we have some color codes that we can use for printing colorful text with background colors as well, in python we have three ways to write a text in color using python modules and library.

  • Colorama module
  • Termcolor Library
  • ANSI code

Colorama Module in Python

This is the most commonly used module in python,  the Colorama module is used to write text in colors with background colors to make the text more eye-catching and readable.

In the Colorama module, we have 3 formatting options.

  1. fore
  2. back
  3. style

These colors can be used as background colors.

  1. RED
  2. GREEN
  3. BLUE
  4. WHITE
  5. YELLOW
  6. MAGENTA
  7. CYAN
  8. WHITE

We can style text in three ways.

  1. DIM
  2. BRIGHT
  3. NORMAL

We can change foreground and background color and the style as well like In which format we want to print the text in Bold, Italic, etc. 

For better understanding check out the example below.

Example

from colorama import init
from colorama import Fore
init()
print(Back.RED + Fore.BLUE + Style.BRIGHT + 'Hello World')
print(Back.GREEN + Fore.YELLOW + Style.BRIGHT + 'Hello World')
print(Back.BLUE + Fore.YELLOW + Style.BRIGHT +'Hello World')
print(Back.YELLOW + Fore.WHITE + Style.BRIGHT +'Hello World')

Code Explanation

In the above code we imported the fore and init first from the Colorama module, there is no need to import back, then we initialized the module and printed the hello world in BLUE foreground color with Green background color. We also added a style option to show the text bright.

Check out the results below.

Output


As you can see above the same text has been printed in four different background and foreground colors.

Let’s move forward to the Termcolor Library.

Termcolor Library

Termcolor is a library in python that does the same task it is also used to print colorful text. Checkout the example below:

Note: Before executing the following code first install the termcolor library by using this “pip install termcolor” command in the command prompt (cmd).

Example

#Import necessary module
from termcolor import colored

#declaring "sentence" variable
sentence = "Color code in python"
print(colored(sentence, 'blue', 'on_yellow'))

Code Explanation

In the above code we imported colored from the termcolor library then we declared a variable named “sentence” and stored a text in it.

After that, we printed the text using a colored function in which we defined the foreground and background color for the sentence.

Check out the results below.

Output

ANSI Code

The full form of ANSI is American National Standards Institute.

Example

import os
os.system("")  

COLOR = {
    "HEADER": "\033[95m",
    "BLUE": "\033[94m",
    "GREEN": "\033[92m",
    "RED": "\033[91m",
    "ENDC": "\033[0m",
}

print(COLOR["BLUE"], "Color Code in Python", COLOR["ENDC"])

In the above code we used the ANSI code of python for printing colorful text, first of all, we imported the os module that will enable ANSI color codes in the terminal then we declared an array and stored some ANSI codes in it then we printed the text in blue color.

Checkout the results below:

Output

Conclusion

In this tutorial, we talked about the color codes in python. We discussed a brief introduction to color codes, we saw the modules and libraries that can be used to print text with foreground and background colors.

We also discussed those methods and understood their examples as well.

That’s all for today hope you like the tutorial if you have any queries regarding this article so feel free to contact us below. Thanks for reading. 

Leave a Comment