how to print colors in python

Master how to print colors in Python – the ultimate step-by-step guide

By Ashley Harris
AD

Printing colors in Python opens a world of visual feedback, transforming dull console output into a vibrant, readable experience. While the standard terminal displays monochrome text, Python provides several straightforward methods to inject color, enhancing debugging, logging, and user interaction. The key lies in understanding how terminals interpret specific escape sequences or leverage external libraries for broader compatibility.

Understanding Terminal ANSI Escape Codes

The foundation for colored text in most modern terminals is the ANSI escape code. These are special sequences of characters that command the terminal to change text color, background color, or style (like bold or underline). Python prints these sequences as raw strings, and the terminal interprets them to apply the requested formatting. This method requires no external dependencies, making it ideal for simple, quick scripts.

Implementing Basic ANSI Codes

To use ANSI codes, you embed them directly into your print strings. A common format is \033[1;31m, which translates to an escape character, followed of bracket, attributes, semicolon, and color code, followed by the text, and closed with \033[0m to reset formatting. Here is a simple example demonstrating red text:

AD
How To Print Colored Text in Python (Colorama Tutorial)
How To Print Colored Text in Python (Colorama Tutorial)
Colour chart for Tkinter and Tix
Colour chart for Tkinter and Tix
Printing Colored Output in Python 🎨
Printing Colored Output in Python 🎨
  • print("\033[1;31mError: File not found\033[0m")
  • print("\033[1;32mSuccess: Operation completed\033[0m")

This direct approach is highly portable across Linux and macOS terminals, though support can vary slightly on older Windows command prompts.

Leveraging the Colorama Library for Cross-Platform Safety

While ANSI codes work well on Unix-based systems, Windows consoles historically required different handling, leading to messy output or missing colors. The colorama library solves this by acting as a compatibility layer, translating ANSI codes into the appropriate Windows API calls. It ensures your colored prints work seamlessly across all major operating systems without modification.

Adding color | How To Print Colored Text in Python | Free Programming Classes | Harisystems
Adding color | How To Print Colored Text in Python | Free Programming Classes | Harisystems
Pattern Printing in Python ⭐ | Beginner Loop Practice Program
Pattern Printing in Python ⭐ | Beginner Loop Practice Program
Different color options in python to print the output on terminal
Different color options in python to print the output on terminal

Installation and Basic Usage

Installing colorama is done via pip. Once installed, you initialize it and then use the same familiar ANSI codes with confidence. The library handles the underlying complexity, allowing you to focus on the colors themselves.

  • pip install colorama
  • from colorama import Fore, Style, init
  • init(autoreset=True) # Automatically resets color after each print
  • print(Fore.RED + "This text is red on any platform")

Utilizing the Termcolor Library for Simplified Syntax

For an even more concise syntax, the termcolor library wraps color handling into a single, intuitive function. This is excellent for quickly adding color to output without manually concatenating style strings. It provides a clean API for foreground colors, background colors, and attributes like bold or blinking.

AD

Key Functions and Attributes

The colored() function is the centerpiece of this library. You pass your text as the first argument and specify the color and attributes as parameters. This method promotes readability and keeps your print statements clean.

Tkinter
Tkinter
an image of a web page with the words printing colored output in python on it
an image of a web page with the words printing colored output in python on it
Python + Art = 🌹 Try This Fun Code!
Python + Art = 🌹 Try This Fun Code!
Python tkinter Toolbox
Python tkinter Toolbox
a computer screen with an image of a flower in the center and text that reads, colorful flowers in python
a computer screen with an image of a flower in the center and text that reads, colorful flowers in python
a computer screen with an image of a spiral in the middle and text on it
a computer screen with an image of a spiral in the middle and text on it
Parameter Description Example
text The string to be colored. "Hello World"
color The foreground color (e.g., red, blue, green). color='green'
on_color The background color (e.g., on_red, on_blue). on_color='on_yellow'
attrs A list of style attributes like 'bold' or 'underline'. attrs=['bold', 'underline']

By mastering these core techniques—from raw ANSI codes to robust libraries like Colorama and Termcolor—you can effectively print colors in Python, making your scripts more informative and user-friendly.

an image of different colored squares in the same pattern, each with different colors and sizes
an image of different colored squares in the same pattern, each with different colors and sizes
Draw a Heart in Python Using Turtle Graphics ❤️
Draw a Heart in Python Using Turtle Graphics ❤️
Learn how to make your own Optical Illusion in Python
Learn how to make your own Optical Illusion in Python
Patterns in Python
Patterns in Python
a computer screen with an image of a spiral in the middle and text reading colorful galaxy using
a computer screen with an image of a spiral in the middle and text reading colorful galaxy using
a computer screen with an image of a spiral in the middle and text that reads,'colorful galaxy using python '
a computer screen with an image of a spiral in the middle and text that reads,'colorful galaxy using python '
GitHub - TomSchimansky/CustomTkinter: A modern and customizable python UI-library based on Tkinter
GitHub - TomSchimansky/CustomTkinter: A modern and customizable python UI-library based on Tkinter
a screen shot of a program with the text python program color name to color code
a screen shot of a program with the text python program color name to color code
a book with an image of a page containing different types of words and numbers on it
a book with an image of a page containing different types of words and numbers on it
python 🐱
python 🐱
an orange and yellow snake on a white background
an orange and yellow snake on a white background
Python
Python
Python Basics Cheat Sheet for Beginners | Learn Coding Step by Step
Python Basics Cheat Sheet for Beginners | Learn Coding Step by Step
How To Program Graphics Using Python Turtle Module
How To Program Graphics Using Python Turtle Module
AD