Skip to main content

Your Terminal Shouldn't Just Be Black and White

Have you ever questioned life while writing a CLI tool or debugging output?

"Why does the information I just printed look like a plain bowl of noodles with no toppings?"

Sorry, I don't mean to offend noodles, I just want to say:

If you want your terminal to be more recognizable and allow you to spot key information at a glance, then you need to get to know a subtle but powerful player:

  • ANSI Escape Codes.

What Are They?

ANSI stands for American National Standards Institute, an organization responsible for setting various standards, including character encoding for computers.

ANSI Escape Codes are sequences of characters that look like random code. They usually begin with \033[ or \x1b[, followed by various control codes.

Here's an example:

print("\033[1;31mHello World\033[0m")

This code will print "Hello World" in bold red text. The \033[0m at the end is a reset code, preventing the following output from being affected by the red color.

Common Formatting Control Codes

You can use these values to style the text:

CodeEffect
0Reset (default)
1Bold
2Dim
3Italic
4Underline
7Invert (reverse)

These can be combined with colors to create a "personalized outfit" for your CLI.

info

Fun Fact About ANSI Control Codes: Where Did 5 and 6 Go?

They've always been around, but most people never see them.

  • 5 = Slow Blink
  • 6 = Rapid Blink

However...

Almost no terminal supports them, especially modern systems that disable blink effects to avoid eye strain.

You can try it yourself:

print("\033[5;31mThis is a slow-blinking red text\033[0m")

If it actually blinks, congratulations! Your terminal is stuck in the ancient times 😅

Color Control Codes

The text color codes in ANSI Escape Codes typically range from 30–37 (standard colors) and 90–97 (bright colors).

Here is the corresponding table:

CodeColor
30Black
31Red
32Green
33Yellow
34Blue
35Magenta
36Cyan
37White
90–97Bright Colors

Background color codes correspond to 40–47 (standard colors) and 100–107 (bright colors), and they follow the same pattern as above:

CodeBackground Color
40Black (Background)
41Red (Background)
42Green (Background)
43Yellow (Background)
44Blue (Background)
45Magenta (Background)
46Cyan (Background)
47White (Background)
100–107Corresponding Bright Background Colors

You may notice that codes from 38 to 89 are rarely mentioned — so where did they go?

  • 38 and 48: Advanced Usage for Custom Colors

    • 38 = Enable "custom foreground color" mode
    • 48 = Enable "custom background color" mode

    At this point, you need to specify the color with additional parameters, such as 256 colors (8-bit mode):

    print("\033[38;5;198mA pink text\033[0m")
    • 38;5;198 refers to color number 198 from the 256-color palette.

    Or, use True Color (24-bit mode):

    print("\033[38;2;255;105;180mHello in pink!\033[0m")
    • 38;2;<r>;<g>;<b> allows you to precisely specify RGB values.
  • 39 and 49: The Reset Color Friends

    • 39: Reset to the default foreground color
    • 49: Reset to the default background color
tip

What About 50 to 89?

They don't mean anything!

This range of values is not explicitly defined in the ANSI specification; it is reserved or part of a historical gap. A few terminals (like certain Konsole/xterm) have experimentally used them, but they are not standard features.

If you force these values, most terminals won’t care.

So, let’s summarize the color code map:

RangeFunction
30–37Standard foreground colors
90–97Bright foreground colors
38Advanced foreground colors (256 colors / RGB)
39Reset foreground color
40–47Standard background colors
100–107Bright background colors
48Advanced background colors (256 colors / RGB)
49Reset background color
50–89⚠️ Reserved, do not touch lightly

Here’s a program to play around with:

for i in range(256):
print(f"\033[48;5;{i}m {i:3} \033[0m", end=" ")
if i % 16 == 15:
print()

After running it, it will look something like this:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255

Combination Syntax Examples

The syntax format is:

\033[<format>;<text color>m

Example:

def colorstr(text, fmt=1, color=34):
return f"\033[{fmt};{color}m{text}\033[0m"

print(colorstr("This is a bold blue text"))

A bit trendier:

print("\033[1;34;43mBlue text on yellow background, fashion explained\033[0m")

Notes

There are a few things to keep in mind when using color so that your output doesn't turn into a disaster:

  • Always reset: Without \033[0m, your output will inherit the color, turning a simple red message into a long red novel.
  • Jupyter has many limitations: While it can display color, cursor control and screen manipulation are mostly a dream.
  • Windows Terminal has been updated: The new version finally does a good job; for the old version, use colorama.
  • Don’t mess with log files: Unless you enjoy torturing yourself with regular expressions, it's better to save the text as plain text.

Extended Applications

You can wrap your own color utility module and even use Enum to write a cleaner call method:

from enum import Enum

class COLOR(Enum):
RED = 31
GREEN = 32
BLUE = 34

class FORMAT(Enum):
BOLD = 1
UNDERLINE = 4

def colorstr(obj, color=COLOR.BLUE, fmt=FORMAT.BOLD):
return f"\033[{fmt.value};{color.value}m{obj}\033[0m"

Application scenarios:

  • Error and warning messages: Use red to say “There’s a problem here.”
  • Success messages: A green checkmark is just delightful.
  • Interactive menu prompts: Let the user know what to enter next.

Don’t Want to Handle It Yourself?

Many libraries have already packaged this for you, so you can use them directly.

Here are a few Python libraries you can refer to:

  1. colorama

    Solves cross-platform display issues, a Windows helper, and is easy to use:

    from colorama import init, Fore, Style
    init()
    print(Fore.RED + "Hello in red!" + Style.RESET_ALL)
  2. termcolor

    Provides colored(), no fuss:

    from termcolor import colored
    print(colored("Warning", "red", attrs=["bold"]))
  3. rich

    Supports colors, tables, progress bars, Markdown—it's like React for CLI.

    from rich import print
    print("[bold red]This is a bold red text[/bold red]")

Summary

Mastering ANSI Escape Codes is like adding a color palette to your terminal.

From debugging to CLI tool development, you can create the most eye-catching effects with minimal tools.

More importantly, it allows users to quickly recognize "red is an error," "green is good," and "yellow means wait," without getting lost in a sea of black-and-white messages.

Make the terminal world colorful, starting with \033[.

☕ Fuel my writing with a coffee

Your support keeps my AI & full-stack guides coming.

cta-button
AI Consulting icon
AI

AI Consulting

From research to deployment, bring your AI ideas to fast and practical.

What You’ll Get
  • Tech insights
  • Tailored modeling
  • PoC & trend guidance
Full-Stack Dev icon
FullStack

Full-Stack Dev

Frontend, backend, and deployment in one pipeline, built for scale.

Tech Stack
  • FastAPI + Docker
  • React
  • CI/CD automation
Custom Solutions icon
Custom

Custom Solutions

Flexible, future-ready setups for your unique needs.

Scope
  • Project scoping & PoC
  • MVP development
  • Scaling & support

🚀 Ready for your next project?

Need a tech partner or custom solution? Let’s connect.