ANSI colors for Python

1.1.8 · maintenance · verified Thu Apr 09

The `ansicolors` library provides a simple way to add ANSI escape codes for foreground, background, and text styles to strings for terminal output. It supports 8-color, 256-color (xterm), and 24-bit RGB color definitions. The current version is 1.1.8, released in June 2017, and the project appears to be in maintenance mode due to lack of recent updates.

Warnings

Install

Imports

Quickstart

Demonstrates basic 8-color, 256-color, and 24-bit RGB usage with foreground, background, and style options. Includes a check for terminal color support.

from colors import color
import os

# Basic 8-color
print(color('Hello, ANSI!', fg='green'))
print(color('Warning!', fg='black', bg='yellow', style='bold'))

# Check for color support (common practice)
if os.environ.get('NO_COLOR') is None and os.stdout.isatty():
    # 256-color (xterm) example
    print("\n256-color palette (if supported):")
    for i in range(256):
        if i % 16 == 0:
            print()
        print(color(f'{i:03d}', fg=i), end=' ')
    print('\n')

    # 24-bit RGB example
    print(color('True Color!', fg=(255, 100, 50), style='underline'))
    print(color('Deep Blue Background', bg='#00008b', fg='white'))
else:
    print("\nTerminal does not support ANSI colors or NO_COLOR is set.")

view raw JSON →