How to Add Colorful Text to Your Terminal with ANSI Escape Codes and Python
This guide explains how to use ANSI escape sequences, 16‑color and 256‑color schemes, and the Python Colorama library to produce colored, highlighted, and styled text in terminal applications, complete with code examples and visual illustrations.
In terminal output, the default is monochrome, but you can use ANSI escape sequences to display colored, highlighted, blinking text, move the cursor, clear the screen, and more.
1. ANSI Escape Sequences
Terminals support special output strings that change their behavior; for example, "\n" creates a new line.
2. Color Schemes
Two common schemes are used in terminals: the 16‑color scheme (8 foreground + 8 background) and the 256‑color scheme.
16 Colors
The 16‑color scheme uses a syntax like \033[1;32;40m where 1 selects a bright style, 32 sets the foreground to green, and 40 sets the background to black.
Escape characters can be expressed in hexadecimal ( \x1b[), Unicode ( \u001b[) or octal ( \033[).
Common style codes include 0 (default), 1 (bright), 22 (normal), 4 (underline), 5 (blink), 7 (reverse), etc.
print('\033[0;32;40mThis is a test line\033[0m')
print('\033[1;32;40mThis is a test line\033[0m')
print('\033[22;32;40mThis is a test line\033[0m')
print('\033[4;32;40mThis is a test line\033[0m')
print('\033[5;32;40mThis is a test line\033[0m')
print('\033[7;32;40mThis is a test line\033[0m')Foreground color codes: 30‑37 (black to white). Background color codes: 40‑47.
print('\033[1;30;40mBlack text\033[0m')
print('\033[1;31;40mRed text\033[0m')
print('\033[1;32;40mGreen text\033[0m')
print('\033[1;33;40mYellow text\033[0m')
print('\033[1;34;40mBlue text\033[0m')
print('\033[1;35;40mMagenta text\033[0m')
print('\033[1;36;40mCyan text\033[0m')
print('\033[1;37;40mWhite text\033[0m') print('\033[1;37;40mBlack background\033[0m')
print('\033[1;37;41mRed background\033[0m')
print('\033[1;37;42mGreen background\033[0m')
print('\033[1;37;43mYellow background\033[0m')
print('\033[1;37;44mBlue background\033[0m')
print('\033[1;37;45mMagenta background\033[0m')
print('\033[1;37;46mCyan background\033[0m')
print('\033[1;37;47mWhite background\033[0m')Colorama Module
Colorama simplifies colored output in Python.
from colorama import init, Fore, Back, Style
init(autoreset=True)
print(Style.BRIGHT + Back.YELLOW + Fore.RED + "Sample text")Simple Color Function
background_color_dict={'BLACK':40,'RED':41,'GREEN':42,'YELLOW':43,'BLUE':44,'MAGENTA':45,'CYAN':46,'WHITE':47}
text_color_dict={'BLACK':30,'RED':31,'GREEN':32,'YELLOW':33,'BLUE':34,'MAGENTA':35,'CYAN':36,'WHITE':37}
style_dict={'normal':0,'bold':1,'light':2,'italicize':3,'underline':4,'blink':5}
def set_text_color(str_text, style, text_color, background_color):
style_code=style_dict[style]
text_color_code=text_color_dict[text_color]
back_color_code=background_color_dict[background_color]
return f'\033[{style_code};{text_color_code};{back_color_code}m{str_text}\033[0m'256 Colors
The 256‑color scheme uses 38;5; for foreground and 48;5; for background.
print("\033[48;5;160m\033[38;5;231mBackground and foreground\033[0m")Example script prints all 256 colors:
def print_colors_256(color_code):
num1=str(color_code)
num2=str(color_code).ljust(3,' ')
if color_code % 16 == 0:
return f"\033[38;5;{num1}m {num2} \033[0;0m
"
else:
return f"\033[38;5;{num1}m {num2} \033[0;0m"
print("256 color scheme:")
print(' '.join([print_colors_256(x) for x in range(256)]))Note that actual appearance may vary depending on the terminal emulator and its theme.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
