ANSI Terminal Color Formatting

3.3.0 · active · verified Sat Mar 28

Termcolor is a Python library that provides ANSI color formatting for output in the terminal. It supports 16 colors, 256 colors, and 24-bit true color modes, along with various text attributes like bold, underline, and italic. It is actively maintained with frequent updates, currently at version 3.3.0.

Warnings

Install

Imports

Quickstart

Demonstrates basic text coloring, background colors, text attributes (bold, underline, italic), and true color (RGB) support. It also shows how to print to `sys.stderr`.

import sys
from termcolor import colored, cprint
import os

# Basic colored text
print(colored("Hello, World!", "red"))

# Colored text with background and attributes
cprint("Hello, colorful world!", "green", "on_yellow", attrs=["bold", "underline"])

# True color (RGB) example (requires a compatible terminal and termcolor >= 3.1.0)
# You can also specify 0-255 RGB ints via a tuple for color and on_color
if os.environ.get('FORCE_COLOR', '0') == '1': # Check for forced color for consistent demo
    cprint("True color text!", (100, 150, 250), (50, 60, 70), attrs=["italic"])
else:
    print(colored("Italic attribute and true color (RGB) requires termcolor >= 3.3.0 and >= 3.1.0 respectively, and a compatible terminal.", "white", attrs=["dark"]))

# Print to stderr
cprint("This is an error message.", "red", attrs=["reverse"], file=sys.stderr)

view raw JSON →