colored text
print
coding
terminal

How do I print colored text to the terminal?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In Python, you can print colored text to the terminal using several methods. Here are the most popular approaches:


1. Using ANSI Escape Codes

ANSI escape codes are special sequences used to control text formatting, including color, boldness, and background.

Syntax:

python
print("\033[<style>;<text-color>;<background-color>mYour Text\033[0m")
  • \033[ : Escape sequence to start.
  • m : Ends the color/style formatting.
  • 0 resets the style after the colored text.

Example:

python
1# Colored text using ANSI escape codes
2print("\033[31mThis is red text\033[0m")  # 31 = red
3print("\033[32mThis is green text\033[0m")  # 32 = green
4print("\033[33mThis is yellow text\033[0m")  # 33 = yellow
5print("\033[34mThis is blue text\033[0m")  # 34 = blue
6print("\033[1;35mThis is bold magenta text\033[0m")  # 1 = bold, 35 = magenta
7print("\033[1;37;44mWhite text on blue background\033[0m")  # 37 = white text, 44 = blue background

Common Codes:

  • Text Colors:
    • 30: Black, 31: Red, 32: Green, 33: Yellow
    • 34: Blue, 35: Magenta, 36: Cyan, 37: White
  • Styles:
    • 0: Reset, 1: Bold, 4: Underline, 7: Inverted colors
  • Background Colors:
    • 40: Black, 41: Red, 42: Green, 43: Yellow
    • 44: Blue, 45: Magenta, 46: Cyan, 47: White

2. Using the colorama Library (Cross-Platform)

colorama is a library that makes it easy to print colored text in a cross-platform way, especially on Windows where ANSI codes are not natively supported.

Installation:

bash
pip install colorama

Example:

python
1from colorama import init, Fore, Back, Style
2
3# Initialize colorama (required for Windows)
4init(autoreset=True)
5
6# Print colored text
7print(Fore.RED + "This is red text")
8print(Fore.GREEN + "This is green text")
9print(Back.YELLOW + "Yellow background")
10print(Style.BRIGHT + Fore.BLUE + "Bright blue text")
11print(Fore.WHITE + Back.BLUE + "White text on blue background")

Output:

  • Fore sets text color.
  • Back sets background color.
  • Style adds effects like bold (e.g., Style.BRIGHT).

3. Using the termcolor Library

termcolor simplifies adding colors to terminal text.

Installation:

bash
pip install termcolor

Example:

python
1from termcolor import colored
2
3# Print colored text
4print(colored("This is red text", "red"))
5print(colored("This is green text", "green"))
6print(colored("Yellow text on blue background", "yellow", "on_blue"))
7print(colored("Magenta text with bold style", "magenta", attrs=["bold"]))

Output:

  • colored(text, color, on_color, attrs) formats the text.
  • Supported colors: 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'.

4. Using the rich Library (Advanced Features)

rich is a powerful library for rich text formatting, including color, bold, tables, and progress bars.

Installation:

bash
pip install rich

Example:

python
1from rich.console import Console
2
3console = Console()
4
5# Print colored text
6console.print("This is [bold red]red and bold[/] text")
7console.print("This is [green]green[/] text")
8console.print("This is [yellow on blue]yellow text on blue background[/]")

Output:

rich uses a markup syntax similar to HTML for text formatting.


Summary Table

MethodLibrary RequiredProsExample
ANSI CodesNoneSimple, no extra library needed\033[31mRed\033[0m
coloramaYesCross-platform, easy to useFore.RED + "Text"
termcolorYesLightweight, clean APIcolored("Text", "red")
richYesAdvanced features (tables, markup)console.print("[red]Text")

Recommendation:

  • Use colorama for basic, cross-platform terminal colors.
  • Use rich for advanced text formatting and terminal enhancements.
  • Use ANSI escape codes for quick, lightweight color changes without libraries. 🚀

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions