How to Output Colored Text in the Terminal Using ANSI Escape Sequences and Colorama
This article explains how to use ANSI escape sequences and the Colorama library in Python to display colored, styled, and background‑colored text in terminal windows, covering 16‑color and 256‑color schemes, escape character formats, style codes, and reusable functions with code examples.
In terminal output the default monochrome text can be enhanced by using ANSI escape sequences to change text color, background, and style.
The article explains the structure of ANSI codes, the two common color schemes (16‑color and 256‑color), and the three ways to write escape characters (hexadecimal, Unicode, octal).
It provides examples of 16‑color codes, describing style values such as bold, underline, and blink, and shows Python print statements that produce colored text.
<code>print('\033[0;32;40m这是一行测试字体\033[0m')
print('\033[1;32;40m这是一行测试字体\033[0m')
print('\033[22;32;40m这是一行测试字体\033[0m')
print('\033[4;32;40m这是一行测试字体\033[0m')
print('\033[24;32;40m这是一行测试字体\033[0m')
print('\033[5;32;40m这是一行测试字体\033[0m')
print('\033[25;32;40m这是一行测试字体\033[0m')
print('\033[7;32;40m这是一行测试字体\033[0m')
print('\033[27;32;40m这是一行测试字体\033[0m')</code>The Colorama library is introduced to simplify color handling in Python, with sample initialization and usage code.
<code>from colorama import init, Fore, Back, Style
init(autoreset=True)
print(Style.BRIGHT + Back.YELLOW + Fore.RED + "Sample colored text")
</code>A reusable set_text_color function is presented, mapping human‑readable color and style names to their corresponding ANSI codes.
<code>def set_text_color(str_text, style, text_color, background_color):
str = str_text
style_code = style_dict[style]
text_color_code = text_color_dict[text_color]
back_color_code = background_color_dict[background_color]
print_text = f'\033[{style_code};{text_color_code};{back_color_code}m{str}\033[0m'
return print_text
</code>A print_colors_256 function iterates through all 256 colors, demonstrating how to output each foreground color.
<code>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\n"
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)]))
</code>Finally, the article notes terminal compatibility issues and includes images illustrating the visual results of the colored output.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.