Print colored text in a Python terminal
Use ANSI escape sequences, Colorama, or Rich, and keep terminal styling separate from plain output sent to files and log collectors.
ANSI escape sequences can style text in terminals that support them. Colorama helps with Windows console compatibility, while Rich offers a higher-level interface for styled text, tables, progress displays, and other terminal output.
Use ANSI sequences directly
def colored(text, color):
colors = {
"red": "\033[91m",
"green": "\033[92m",
"yellow": "\033[93m",
"blue": "\033[94m",
"cyan": "\033[96m",
}
return f"{colors[color]}{text}\033[0m"
print(colored("Operation failed", "red"))
print(colored("Completed", "green"))
The final \033[0m resets styling so it does not leak into later output. Standard foreground codes are 30–37 and bright foreground codes are 90–97; background ranges are 40–47 and 100–107. \033[1m commonly requests bold or increased intensity. Actual appearance depends on the terminal and its theme.
Use Colorama for console compatibility
python -m pip install colorama
With Colorama 0.4.6 or later:
from colorama import just_fix_windows_console, Fore, Style
just_fix_windows_console()
print(Fore.RED + "Red text" + Style.RESET_ALL)
print(Fore.GREEN + "Green text" + Style.RESET_ALL)
just_fix_windows_console() enables or adapts Windows console handling where needed and is safe to call repeatedly. Older examples use init(), which also has stream-wrapping behavior; it is not a universal requirement on every modern terminal. Neither function makes all redirected destinations understand ANSI codes.
Use Rich for richer terminal output
python -m pip install rich
from rich import print as rprint
rprint("[bold red]Error[/bold red] The operation failed")
rprint("[green]Success[/green] The task completed")
Rich detects terminal capabilities and normally removes terminal control sequences when output is redirected. When inserting arbitrary user text, avoid treating it as trusted Rich markup: escape it or use Console.print(text, markup=False) as appropriate.
Keep redirected output plain
For a manual ANSI helper, a basic policy is:
import sys
message = "Operation failed"
print(colored(message, "red") if sys.stdout.isatty() else message)
isatty() is a useful signal, not a complete capability test. Some terminals disable color, and some nonterminal renderers understand it. Honor an application's no-color option and the behavior of your terminal library. Raw escapes stored in a log file are extra control characters, not necessarily an encoding failure.
FAQ
Can logging have colored output? Use a terminal-specific formatter or a package such as colorlog for the console handler. Keep file and structured-log handlers uncolored so collectors do not need to remove styling.
Does this work in notebooks? It depends on the frontend and output type. Many notebook frontends render common ANSI sequences; HTML display is another option in IPython. A terminal's capabilities should not be assumed to match every notebook or exported document.
References
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan