# Print colored text to terminal with Python

**URL:** https://sentry.io/answers/print-colored-text-to-terminal-with-python/

---

## 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](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors). 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:

```python
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:

```python
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 = '\033[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:

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

Many additional colors can be accessed using [8-bit color codes](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit).

On older versions of Windows using the legacy console (conhost.exe), ANSI escape sequences are not supported by default. To enable them, add the following lines to the start of the script:

```python
import os

os.system('color')
```

Windows Terminal (the default on Windows 11) supports ANSI sequences natively and does not require this workaround.

## Using a Library

If we would prefer to abstract away from ANSI codes, we can use the [`termcolor`](https://pypi.org/project/termcolor/) library from PyPI. First, we must install it:

```bash
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:

```python
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, an optional background color as the third argument (e.g. `'on_red'`), and an optional list of attributes (e.g. `['bold', 'underline']`) as the fourth argument.

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

```bash
python -m termcolor
```

As with the previous method, the `os.system('color')` workaround may be needed on Windows when using the legacy console (see above).

---

*Source: [sentry.io/answers/print-colored-text-to-terminal-with-python/](https://sentry.io/answers/print-colored-text-to-terminal-with-python/)*
