How to Print Colored Text in Python
In this article, we will learn to print colored text in Python. We will use some built-in modules and libraries and some custom codes as well. Let's first have a quick look over how Python represents color codes.
In the Python programming language, text can be represented using different colors. There are very simple to use Python libraries for colors and formatting in the terminal. The programmer gets better responses by printing colored texts.
Let's see some useful examples to color text in Python.
Print Color Text using colorma Module
We can use the built-in colorama
module of Python to print colorful text. It is a cross-platform printing module. In this, colored text can be done using Colorama’s
constant shorthand for ANSI
escape sequences. Just import from coloroma module and get your desired output.
import colorama
from colorama import Fore
print(Fore.RED + 'This text is red in color')
This text is red in color
Print Color Text using termcolor Module
We can use the built-in termcolor module to print text color in Python. The termcolor is a python module for ANSII Color formatting for output in the terminal. For this, sys
module has to be imported first and then colored from termcolor module as given below.
import sys
from termcolor import colored, cprint
text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)
Hello, World!
Print Color Text using ANSI Code in Python
We can use ANSI code style to make your text more readable and creative, you can use ANSI escape codes to change the color of the text output in the python program. A good use case for this is to highlight errors. The escape codes are entered right into the print statement.
print("\033[1;32m This text is Bright Green \n")
This text is Bright Green
The above ANSI escape code will set the text color to bright green. The format is;
- \033[ = Escape code, this is always the same
- 1 = Style, 1 for normal.
- 32 = Text colour, 32 for bright green.
Print Color Text using the colored module
We can use the colored module and its functions to color text in Python. It is a library that can be used after installing by using the pip command. So, first, install it and then import it into your python script to highlight text colors.
from colored import fg
print ('%s Hello World !!! %s' % (fg(1), attr(0)))
Hello World !!!
Example 2
We can pass the name of the color into the fg() function as well. See, it prints text in blue color as we passed blue as value.
from colored import fg
color = fg('blue')
print (color + 'Hello World !!!')
Hello World !!!
These are the different ways in which you can print your text in different colors. You can also add different styles to your text, different background colors to your text as well.
Conclusion
In this article, we learned to color text and print colored background as well by using several built-in functions such as coloroma
module, termcolor
, colored
module etc. We used some custom codes as well. For example, we used different colors and text to highlight and print colored text.