ANSI color formatting for output in terminal
npx @tessl/cli install tessl/pypi-termcolor@3.1.0ANSI color formatting for output in terminal. Termcolor provides simple functions like colored() and cprint() to add colors, background colors, and text attributes (bold, underline, blink, etc.) to console text.
pip install termcolorfrom termcolor import colored, cprintImport constants and utilities:
from termcolor import COLORS, HIGHLIGHTS, ATTRIBUTES, RESETFull import:
from termcolor import ATTRIBUTES, COLORS, HIGHLIGHTS, RESET, colored, cprintfrom termcolor import colored, cprint
# Using colored() to format text
text = colored("Hello, World!", "red", attrs=["reverse", "blink"])
print(text)
# Using cprint() to print directly
cprint("Hello, World!", "green", "on_red")
# Create reusable formatting functions
print_red_on_cyan = lambda x: cprint(x, "red", "on_cyan")
print_red_on_cyan("Hello, World!")
print_red_on_cyan("Hello, Universe!")
# Using RGB colors
cprint("Custom purple text", (255, 0, 255))
cprint("Light pink text", (255, 182, 193))
# Multiple attributes
cprint("Bold underline reverse cyan", "cyan", attrs=["bold", "underline", "reverse"])Apply colors to text using predefined color names or custom RGB values.
def colored(
text: object,
color: str | tuple[int, int, int] | None = None,
on_color: str | tuple[int, int, int] | None = None,
attrs: Iterable[str] | None = None,
*,
no_color: bool | None = None,
force_color: bool | None = None,
) -> str:
"""
Colorize text with ANSI color codes and return formatted string.
Available text colors:
black, red, green, yellow, blue, magenta, cyan, white,
light_grey, dark_grey, light_red, light_green, light_yellow, light_blue,
light_magenta, light_cyan.
Available text highlights:
on_black, on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white,
on_light_grey, on_dark_grey, on_light_red, on_light_green, on_light_yellow,
on_light_blue, on_light_magenta, on_light_cyan.
Available attributes:
bold, dark, underline, blink, reverse, concealed, strike.
Parameters:
- text: Text to colorize (converted to string)
- color: Text color (string from COLORS dict or RGB tuple with values 0-255)
- on_color: Background color (string from HIGHLIGHTS dict or RGB tuple with values 0-255)
- attrs: Text attributes (iterable of strings from ATTRIBUTES dict)
- no_color: Override to disable colors
- force_color: Override to enable colors
Returns:
Formatted string with ANSI codes
Example:
colored('Hello, World!', 'red', 'on_black', ['bold', 'blink'])
colored('Hello, World!', 'green')
colored('Hello, World!', (255, 0, 255)) # Purple
"""Print colorized text directly to stdout.
def cprint(
text: object,
color: str | tuple[int, int, int] | None = None,
on_color: str | tuple[int, int, int] | None = None,
attrs: Iterable[str] | None = None,
*,
no_color: bool | None = None,
force_color: bool | None = None,
**kwargs: Any,
) -> None:
"""
Print colorized text directly to stdout.
Parameters: Same as colored() plus any kwargs accepted by print()
Returns: None (prints to stdout)
"""Predefined dictionaries mapping color names to ANSI codes.
COLORS: dict[str, int]Available colors: black, grey, red, green, yellow, blue, magenta, cyan, light_grey, white, dark_grey, light_red, light_green, light_yellow, light_blue, light_magenta, light_cyan.
Note: grey is an alias for black.
HIGHLIGHTS: dict[str, int]Available background colors: on_black, on_grey, on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_light_grey, on_white, on_dark_grey, on_light_red, on_light_green, on_light_yellow, on_light_blue, on_light_magenta, on_light_cyan.
Note: on_grey is an alias for on_black.
ATTRIBUTES: dict[str, int]Available text attributes: bold, dark, underline, blink, reverse, concealed, strike.
ANSI reset sequence to clear text formatting.
RESET: strThe ANSI reset sequence "\033[0m" used to reset text formatting.
from collections.abc import Iterable
from typing import AnyTermcolor respects standard environment variables and provides override options:
Environment Variables:
NO_COLOR: Disables colors when setANSI_COLORS_DISABLED: Disables colors when setFORCE_COLOR: Enables colors when setTERM: Disables colors when set to "dumb"Override Parameters:
no_color=True: Disable colors for this callforce_color=True: Enable colors for this callpython -m termcolorDisplays a comprehensive demo showing all available colors, highlights, attributes, and RGB functionality.
Both color and on_color parameters accept RGB tuples with values 0-255:
from termcolor import cprint
# Custom colors using RGB tuples
cprint("Pure red text", (255, 0, 0))
cprint("Pure green text", (0, 255, 0))
cprint("Pure blue text", (0, 0, 255))
cprint("Light pink text", (255, 182, 193))
cprint("Custom purple text", (255, 0, 255))
# RGB background colors
cprint("Text on custom background", "white", (64, 128, 192))