Print colored text to terminal with Python

David Y.

The Problem

How can I print colored text to the terminal in Python?

The Solution

On most platforms, terminal colors are produced using ANSI escape sequences. We can use these directly, or through a Python library.

Using ANSI Sequences

Terminal output can be colored by surrounding it with ANSI escape sequences:

BLACK = '\033[30m' RED = '\033[31m' GREEN = '\033[32m' YELLOW = '\033[33m' # orange on some systems BLUE = '\033[34m' MAGENTA = '\033[35m' CYAN = '\033[36m' LIGHT_GRAY = '\033[37m' DARK_GRAY = '\033[90m' BRIGHT_RED = '\033[91m' BRIGHT_GREEN = '\033[92m' BRIGHT_YELLOW = '\033[93m' BRIGHT_BLUE = '\033[94m' BRIGHT_MAGENTA = '\033[95m' BRIGHT_CYAN = '\033[96m' WHITE = '\033[97m' RESET = '\033[0m' # called to return to standard terminal text color print(BLACK + "black" + RESET) print(RED + "red" + RESET) print(GREEN + "green" + RESET) print(YELLOW + "yellow" + RESET) print(BLUE + "blue" + RESET) print(MAGENTA + "magenta" + RESET) print(CYAN + "cyan" + RESET) print(LIGHT_GRAY + "light gray" + RESET) print(DARK_GRAY + "dark gray" + RESET) print(BRIGHT_RED + "bright red" + RESET) print(BRIGHT_GREEN + "bright green" + RESET) print(BRIGHT_YELLOW + "bright yellow" + RESET) print(BRIGHT_BLUE + "bright blue" + RESET) print(BRIGHT_MAGENTA + "bright magenta" + RESET) print(BRIGHT_CYAN + "bright cyan" + RESET) print(WHITE + "white" + RESET)

This script will print text in the standard terminal colors. The actual colors displayed when running the script will vary based on the terminal’s configuration and support for ANSI colors. As noted in a comment, the YELLOW color may be rendered as orange on some systems.

ANSI codes to give text-colored backgrounds are also available:

BACKGROUND_BLACK = '\033[40m' BACKGROUND_RED = '\033[41m' BACKGROUND_GREEN = '\033[42m' BACKGROUND_YELLOW = '\033[43m' # orange on some systems BACKGROUND_BLUE = '\033[44m' BACKGROUND_MAGENTA = '\033[45m' BACKGROUND_CYAN = '\033[46m' BACKGROUND_LIGHT_GRAY = '\third-party033[47m' BACKGROUND_DARK_GRAY = '\033[100m' BACKGROUND_BRIGHT_RED = '\033[101m' BACKGROUND_BRIGHT_GREEN = '\033[102m' BACKGROUND_BRIGHT_YELLOW = '\033[103m' BACKGROUND_BRIGHT_BLUE = '\033[104m' BACKGROUND_BRIGHT_MAGENTA = '\033[105m' BACKGROUND_BRIGHT_CYAN = '\033[106m' BACKGROUND_WHITE = '\033[107m'

Codes can be combined for multiple effects:

print(GREEN + BACKGROUND_RED + "green on red" + RESET)

Many additional colors can be accessed using 8-bit color codes.

Note that to output colors on Windows, we must add the following lines to the start of the script:

import os os.system('color')

Using a Library

If we would prefer to abstract away from ANSI codes, we can use the termcolor library from PyPI. First, we must install it:

pip install termcolor

We can then use the colored function from this library to print colored text. The script below will print the same output as the one above:

from termcolor import colored print(colored("black", "black")) print(colored("red", "red")) print(colored("green", "green")) print(colored("yellow", "yellow")) print(colored("blue", "blue")) print(colored("magenta", "magenta")) print(colored("cyan", "cyan")) print(colored("light gray", "light_grey")) print(colored("dark gray", "dark_grey")) print(colored("bright red", "light_red")) print(colored("bright green", "light_green")) print(colored("bright yellow", "light_yellow")) print(colored("bright blue", "light_blue")) print(colored("bright magenta", "light_magenta")) print(colored("bright cyan", "light_cyan")) print(colored("white", "white"))

The colored function takes a text string as its first argument, a color as the second argument, and an optional list of attributes (e.g. 'bold', 'underline') as the third argument. To create text with a colored background, we can prepend the color argument with on_ (e.g. on_light_yellow).

A complete list of output formatting supported by termcolor can be viewed by running its demo:

python -m termcolor

As with the previous method, to output colors on Windows, we must add the following lines to the start of our script:

import os os.system('color')

Get Started With Sentry

Get actionable, code-level insights to resolve Python performance bottlenecks and errors.

  1. Create a free Sentry account

  2. Create a Python project and note your DSN

  3. Grab the Sentry Python SDK

pip install --upgrade sentry-sdk
  1. Configure your DSN
import sentry_sdk sentry_sdk.init( "https://<key>@sentry.io/<project>", # Set traces_sample_rate to 1.0 to capture 100% # of transactions for performance monitoring. # We recommend adjusting this value in production. traces_sample_rate=1.0, )

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

    TwitterGitHubDribbbleLinkedinDiscord
© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.