or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/pypi-termcolor

ANSI color formatting for output in terminal

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/termcolor@3.1.x

To install, run

npx @tessl/cli install tessl/pypi-termcolor@3.1.0

index.mddocs/

Termcolor

ANSI 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.

Package Information

  • Package Name: termcolor
  • Package Type: pypi
  • Language: Python
  • Installation: pip install termcolor

Core Imports

from termcolor import colored, cprint

Import constants and utilities:

from termcolor import COLORS, HIGHLIGHTS, ATTRIBUTES, RESET

Full import:

from termcolor import ATTRIBUTES, COLORS, HIGHLIGHTS, RESET, colored, cprint

Basic Usage

from 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"])

Capabilities

Text Coloring

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
    """

Direct Printing

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)
    """

Color Constants

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.

Reset Sequence

ANSI reset sequence to clear text formatting.

RESET: str

The ANSI reset sequence "\033[0m" used to reset text formatting.

Types

from collections.abc import Iterable
from typing import Any

Color Control

Termcolor respects standard environment variables and provides override options:

  • Environment Variables:

    • NO_COLOR: Disables colors when set
    • ANSI_COLORS_DISABLED: Disables colors when set
    • FORCE_COLOR: Enables colors when set
    • TERM: Disables colors when set to "dumb"
  • Override Parameters:

    • no_color=True: Disable colors for this call
    • force_color=True: Enable colors for this call

Command Line Usage

python -m termcolor

Displays a comprehensive demo showing all available colors, highlights, attributes, and RGB functionality.

RGB Color Support

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))

Error Handling

  • KeyError: Raised when invalid color, highlight, or attribute names are used
  • TypeError: Raised when invalid parameter types are passed
  • io.UnsupportedOperation: Handled internally during terminal detection

Platform Compatibility

  • Cross-platform support (Windows, macOS, Linux)
  • Automatic terminal capability detection
  • Graceful fallback when colors are not supported
  • Proper TTY detection and handling