Open In App

Print Colors in Python terminal

Last Updated : 20 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We can print colored text in the terminal using various methods in Python. This is useful for highlighting important output like success messages, warnings, errors, or logs. For example:

Python
print("\033[92mHello \033[91mWorld\033[0m")

This line prints "Hello" in green and "World" in red using ANSI codes.

Using colorama Module

The colorama module enables cross-platform support for printing colored text in the terminal using ANSI escape sequences. It works well on Windows (which doesn’t support ANSI codes by default) and wraps the codes in a way that's consistent across OS.

Example 1: Print red text with green background.

Python
from colorama import Fore, Back, Style
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

Output: 

red text with green background.

Example 2: Print green text with red background.

Python
from colorama import init
from termcolor import colored

init()

print(colored('Hello, World!', 'green', 'on_red'))

Output: 

green text with red background

Explanation:

  • Fore.RED sets text color to red.
  • Back.GREEN sets background color to green.
  • Style.DIM lowers the text intensity (dim effect).
  • Style.RESET_ALL resets everything (color + style).
  • After resetting, the last line prints normally.

Using termcolor Module

The termcolor module is a simpler way to apply text color, background color, and styles like bold or underline using the colored() function or the cprint() function for direct printing.

Python
import sys
from termcolor import colored, cprint

text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)
cprint('Hello, World!', 'green', 'on_red')

print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan')
print_red_on_cyan('Hello, World!')
print_red_on_cyan('Hello, Universe!')

for i in range(10):
    cprint(i, 'magenta', end=' ')

cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr)

Output: 

Printing Colored Text in Python Using ANSI Escape Codes

We can print colored text in the terminal using ANSI escape codes. These codes tell the terminal to render specific foreground colors (like red or green), background colors, and text styles (like bold or underline). Here’s a simple implementation using Python functions for each color.

Python
def prRed(s): print("\033[91m {}\033[00m".format(s))
def prGreen(s): print("\033[92m {}\033[00m".format(s))
def prYellow(s): print("\033[93m {}\033[00m".format(s))
def prLightPurple(s): print("\033[94m {}\033[00m".format(s))
def prPurple(s): print("\033[95m {}\033[00m".format(s))
def prCyan(s): print("\033[96m {}\033[00m".format(s))
def prLightGray(s): print("\033[97m {}\033[00m".format(s))
def prBlack(s): print("\033[90m {}\033[00m".format(s))  # Corrected from 98 to 90 (standard ANSI)

# Calling the functions to print text in different colors
prCyan("Hello World, ")
prYellow("It's")
prGreen("Geeks")
prRed("For")
prGreen("Geeks")

Output: 

Explanation:

Each function above is responsible for printing text in a specific color using ANSI escape codes:

  • \033[ starts the ANSI escape sequence.
  • 91m, 92m, ... etc., are color codes for different foreground colors.
  • \033[00m resets the text style so the terminal returns to normal formatting after each print.

ANSI Color Chart Generator in Python

This script prints a full combination of foreground and background ANSI color codes. It is especially useful for developers who want to visualize available terminal colors and understand how to combine them using ANSI sequences.

Python
for style in [0, 1]:  # 0: normal, 1: bold/bright
    for fg in range(30, 38): 
        for bg in range(40, 48):
            code = f"{style};{fg};{bg}"
            print(f"\033[{code}m {code} \033[0m", end=" ")
        print()  # Newline after each row
    print()  # Extra newline between normal and bold

Output: 

Explanation:

  • Each box in the output displays a different combination of Style (normal or bold), Foreground color and Background color
  • The ANSI code shown in each box is the exact code used to style that box. For example: 0;31;40 is Normal Red text on Black background and 1;36;43 is Bold Cyan text on Yellow background.

Article Tags :
Practice Tags :

Similar Reads