Signup/Sign In

How to Print Colored Text in Python

In this article, you will learn how to print colored text in Python. We will use some built-in modules and libraries and some custom Python code as well to print color text in Python. Let's first have a quick look at 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. Your program or script's output will look better if you print colored texts.

Let's see some code examples to print color text in Python.

1. Print color text using Colorama Package

  • You can use the Colorama package of Python to print colorful text on the terminal.

  • To install the Colorama package, you can use pip install colorama command.

  • The Colorama module provides a constant shorthand for ANSI escape sequences that can be used for colored text in Python.

  • Just import the Fore class from the Coloroma package and start using colors in your print() function.

from colorama import Fore

print(Fore.RED + 'This text is red in color')

This text is red in color

There are other classes too in the Colorama package that you can use to style text in Python.

  1. Back - Add background color to the text

  2. Style - Add some style like dim the text.

Let's see a code example,

from colorama import  Back, Style

print(Back.GREEN + 'The text with Green background')
print(Style.DIM + 'The text is DIM now')

Here is a list of values that you can try:


Enjoy colored text in the Python terminal.

2. Print color text using termcolor Package

  • You can use the termcolor package also to print text color in Python.

  • The termcolor is a Python package for ANSII Color formatting for the text output in the terminal.

  • For using the termcolor package, you need the sys package imported first in your Python script and then use the colored method from termcolor module as given below:

import sys
from termcolor import colored, cprint

text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])

Hello, World!

You can also add more effects to the text on the terminal using the termcolor package.

3. Print color text using ANSI code in Python

  • You 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 programs.

  • 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 color, 32 for bright green.

Here is a list of other colors:

print("\033[1;37;40m \033[2;37:40m TextColour BlackBackground          TextColour GreyBackground                WhiteText ColouredBackground\033[0;37;40m\n")
print("\033[1;30;40m Dark Gray      \033[0m 1;30;40m            \033[0;30;47m Black      \033[0m 0;30;47m               \033[0;37;41m Black      \033[0m 0;37;41m")
print("\033[1;31;40m Bright Red     \033[0m 1;31;40m            \033[0;31;47m Red        \033[0m 0;31;47m               \033[0;37;42m Black      \033[0m 0;37;42m")
print("\033[1;32;40m Bright Green   \033[0m 1;32;40m            \033[0;32;47m Green      \033[0m 0;32;47m               \033[0;37;43m Black      \033[0m 0;37;43m")
print("\033[1;33;40m Yellow         \033[0m 1;33;40m            \033[0;33;47m Brown      \033[0m 0;33;47m               \033[0;37;44m Black      \033[0m 0;37;44m")
print("\033[1;34;40m Bright Blue    \033[0m 1;34;40m            \033[0;34;47m Blue       \033[0m 0;34;47m               \033[0;37;45m Black      \033[0m 0;37;45m")
print("\033[1;35;40m Bright Magenta \033[0m 1;35;40m            \033[0;35;47m Magenta    \033[0m 0;35;47m               \033[0;37;46m Black      \033[0m 0;37;46m")
print("\033[1;36;40m Bright Cyan    \033[0m 1;36;40m            \033[0;36;47m Cyan       \033[0m 0;36;47m               \033[0;37;47m Black      \033[0m 0;37;47m")
print("\033[1;37;40m White          \033[0m 1;37;40m            \033[0;37;40m Light Grey \033[0m 0;37;40m               \033[0;37;48m Black      \033[0m 0;37;48m")

color text in Python output

4. Print color text using the colored package

  • You can use the colored package and its functions to color text in Python.

  • It is a library that can be used after installation 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 !!!

4. 1 Code Example using colored package

We can pass the name of the color into the fg() function.

The code example below prints text in blue color as we passed blue as the 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 in Python. You can also add different styles to your text, different background colors to your text as well.

You learned how to color text in Python and print colored backgrounds on the terminal using several packages such as coloroma package, termcolor package, colored package etc.

About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.