Oven logo

Oven

Published

ANSI color formatting for output in terminal

pip install termcolor

Package Downloads

Weekly DownloadsMonthly Downloads

Requires Python

>=3.9

termcolor

PyPI version Supported Python versions PyPI downloads GitHub Actions status Codecov Licence Code style: Black Tidelift

Installation

From PyPI

python3 -m pip install --upgrade termcolor

From source

git clone https://github.com/termcolor/termcolor
cd termcolor
python3 -m pip install .

Demo

To see demo output, run:

python3 -m termcolor

Example

import sys

from termcolor import colored, cprint

text = colored("Hello, World!", "red", attrs=["reverse", "blink"])
print(text)
cprint("Hello, World!", "green", "on_red")

print_red_on_cyan = lambda x: cprint(x, "red", "on_cyan")
print_red_on_cyan("Hello, World!")
print_red_on_cyan("Hello, Universe!")

for i in range(10):
    cprint(i, "magenta", end=" ")

cprint("Attention!", "red", attrs=["bold"], file=sys.stderr)

Text properties

Text colorsText highlightsAttributes
blackon_blackbold
redon_reddark
greenon_greenunderline
yellowon_yellowblink
blueon_bluereverse
magentaon_magentaconcealed
cyanon_cyanstrike
whiteon_white
light_greyon_light_grey
dark_greyon_dark_grey
light_redon_light_red
light_greenon_light_green
light_yellowon_light_yellow
light_blueon_light_blue
light_magentaon_light_magenta
light_cyanon_light_cyan

Terminal properties

Terminalbolddarkunderlineblinkreverseconcealed
xtermyesnoyesboldyesyes
linuxyesyesboldyesyesno
rxvtyesnoyesbold/blackyesno
dttermyesyesyesreverseyesyes
teratermreversenoyesrev/redyesno
aixtermnormalnoyesnoyesyes
PuTTYcolornoyesnoyesno
Windowsnonononoyesno
Cygwin SSHyesnocolorcolorcoloryes
Mac Terminalyesnoyesyesyesyes

Overrides

Terminal colour detection can be disabled or enabled in several ways.

In order of precedence:

  1. Calling colored or cprint with a truthy no_color disables colour.
  2. Calling colored or cprint with a truthy force_color forces colour.
  3. Setting the ANSI_COLORS_DISABLED environment variable to any value disables colour.
  4. Setting the NO_COLOR environment variable to any value disables colour.
  5. Setting the FORCE_COLOR environment variable to any value forces colour.
  6. Setting the TERM environment variable to dumb, or using such a dumb terminal, disables colour.
  7. Finally, termcolor will attempt to detect whether the terminal supports colour.