Categories
ansi-colors output python terminal

How do I print colored text to the terminal?

2856

How do I output colored text to the terminal in Python?

5

  • This symbol would make a great colored block: Only problem is that it is extended ASCII, maybe you could get it to work using http://stackoverflow.com/questions/8465226/using-extended-ascii-codes-with-python

    Oct 5, 2013 at 16:14

  • Some terminals also can display Unicode characters. If that is true for your terminal, the possible characters are almost unlimited.

    – ayke

    Nov 19, 2013 at 20:02

  • 8

    This answer came fairly late, but it seems to be the best to me… the ones voted above it require special hacks for Windows whereas this one just works: stackoverflow.com/a/3332860/901641

    Dec 16, 2013 at 16:59

  • How about stackoverflow.com/a/42528796/610569 using pypi.python.org/pypi/lazyme ? (disclaimer: shameless plug)

    – alvas

    Mar 1, 2017 at 10:12


  • If you don’t want to install an extra package, follow this new answer.

    Mar 24, 2021 at 11:41

1063

There is also the Python termcolor module. Usage is pretty simple:

from termcolor import colored

print colored('hello', 'red'), colored('world', 'green')

Or in Python 3:

print(colored('hello', 'red'), colored('world', 'green'))

It may not be sophisticated enough, however, for game programming and the “colored blocks” that you want to do…

To get the ANSI codes working on windows, first run

os.system('color')

12

  • 3

    Since it’s emitting ANSI codes, does it work on Windows (DOS consoles) if ansi.sys is loaded? support.microsoft.com/kb/101875

    – Phil P

    Jul 29, 2011 at 4:16

  • 42

    Just noticed that as of 13/01/2011, it’s now under MIT license

    Oct 28, 2011 at 2:19

  • 16

    doesn’t have unittests (unlike colorama) and not updated since 2011

    Jul 20, 2013 at 19:28

  • 6

    termcolor.COLORS gives you a list of colours

    – akxlr

    Nov 14, 2015 at 2:05


  • 63

    On Windows run os.system('color') first, then the ANSI escape sequences start working.

    – Szabolcs

    Dec 12, 2018 at 16:53

947

The answer is Colorama for all cross-platform coloring in Python.

It supports Python 3.5+ as well as Python 2.7.

And as of January 2021 it is maintained.

Example Code:

from colorama import Fore
from colorama import Style

print(f"This is {Fore.GREEN}color{Style.RESET_ALL}!")

Example Screenshot:
example screenshot

19

  • 453

    As the author of Colorama, thanks for the mention @nbv4. I’ll try and clarify a bit: Colorama aims to let Python programs print colored terminal text on all platforms, using the same ANSI codes as described in many other answers on this page. On Windows, Colorama strips these ANSI characters from stdout and converts them into equivalent win32 calls for colored text. On other platforms, Colorama does nothing. Hence you can use ANSI codes, or modules like Termcolor, and with Colorama, they ‘just work’ on all platforms. Is that idea, anyhow.

    Sep 13, 2010 at 13:22


  • 5

    @Jonathan, This is truly an awesome library! The ability to cross platform color Python output is really really nice and useful. I am providing tools for a library that colors its own console. I can redirect the output of that console to the terminal and colorize the output. Now I can even one up the library and let the user select colors. This will allow color blind people to set things to work so they can actually see the output correctly. Thanks

    Nov 30, 2012 at 13:05


  • 79

    This should be in the standard library… Cross platform colour support is important, I think.

    Jun 28, 2013 at 14:08

  • 10

    Colorama is great! Also have a look at ansimarkup, which is built on colorama and allows you to use a simple tag-based markup (e.g. <b>bold</b>) for adding style to terminal text

    – gvalkov

    Feb 19, 2017 at 17:07


  • 62

    This doesn’t work without calling colorama.init(). Vote up!

    Feb 19, 2018 at 3:32