Typing stubs for termcolor

1.1.6.2 · active · verified Tue Apr 14

This is a PEP 561 type stub package for the 'termcolor' library, providing external type annotations for static analysis tools like mypy, pyright, and PyCharm. The current version is 1.1.6.2, released on March 27, 2023. These stub packages are automatically generated and released by the typeshed internal machinery, typically on a daily cadence, ensuring up-to-date type information for various Python libraries.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `colored` function and `cprint` from the `termcolor` library to output text with foreground colors, background highlights, and text attributes to the terminal. When `types-termcolor` is installed, it enables type checkers to provide hints and validate the usage of these functions, improving code quality and readability.

from termcolor import colored, cprint
import sys

# Basic usage of 'colored'
print(colored('Hello, World!', 'red'))

# Using 'colored' with background color and multiple attributes
text_with_attributes = colored(
    "Hello, beautifully colored World!",
    "green",
    "on_red",
    attrs=["bold", "underline"]
)
print(text_with_attributes)

# Using 'cprint' for direct printing
cprint("This is a blue and bold message.", "blue", attrs=["bold"])

# Example with error handling, printing to stderr in red
try:
    raise ValueError("Something went wrong during processing!")
except ValueError as e:
    cprint(f"Error: {e}", "white", "on_red", attrs=["bold"], file=sys.stderr)

view raw JSON →