I went through your source code and I think the probelm is with the color definition within the dictionary.

If you observe carefully, your dictionary value for a color is something like \033[1,30m for white color. However it should be \033[1;30m. Note that you are using a ',(comma) character instead of ';(semicolon) character. As a test I created a subset of the color dictionary and ran these tests.

>>> color = {'white' :'\033[1;37m', 'yellow':'\033[1;33m', 'off' : '\033[0;0m'}
>>> print color['white'] + 'string' + color['off']
string   #this string is white in color
>>> print color['yellow'] + 'string' + color['off']
string #this string is yellow in color
>>> color['yellow'] = '\033[1,33m' #incorrect color code - using a , character instead of ;
>>> print color['yellow'] + 'string' + color['off']
string   #prints the string in console default color i.e. not in yellow color
>>> 

Hope this helps

Answer from Prahalad Deshpande on Stack Overflow
🌐
GitHub
gist.github.com › minism › 1590432
python terminal colors · GitHub
yep, found the solution by myself: print (fg.BLUE, style.BRIGHT , "Show me your color" , style.RESET_ALL) Copy link · Copy Markdown · print("\033[31mTEST") EASY · Copy link · Copy Markdown · noice · Copy link · Copy Markdown · Thanks G · Copy link ·
🌐
GitHub
gist.github.com › rene-d › 9e584a7dd2935d0f461904b9f2950007
ANSI color codes in Python · GitHub
Hope to hear from you, Piet from NL · Copy link · Copy Markdown · Author · These comments are the best way to comment on issues. Copy link · Copy Markdown · Merci pour les codes couleurs. Copy link · Copy Markdown · For simply white: ...
Discussions

Python: octal escape character \033 from a dictionary value translates in a print statement to a UTF-8 character instead - Stack Overflow
I've been experimenting a little with color output in terminal and Python 2.7.3. The ANSI color codes are always rendered flawlessly in the terminal, with this one little exception which I couldn't pinpoint yet any preciser than to this particular dictionary definition. ... color = { 'white': "\033... More on stackoverflow.com
🌐 stackoverflow.com
Python colored text to the terminal - Stack Overflow
We can set color text or foreground color text in the terminal in Python. I have gone through this SO's answer. Some of the example color code is here class bcolors: HEADER = '\033[95m' OKB... More on stackoverflow.com
🌐 stackoverflow.com
printing - Python output complex line with floats colored by value - Stack Overflow
You can use ASCII color codes at the beginning of your print to change the color, as an example '\033[91m' for RED and '\033[94m' for BLUE. More on stackoverflow.com
🌐 stackoverflow.com
python - How do I print colored text to the terminal? - Stack Overflow
The idea of yachalk is to bring ... to the Python ecosystem. If you're interested in a comparison to other libraries I've started feature comparison on the projects page. In addition, here is a long (but still incomplete) list of alternatives that came up during my research -- a lot to choose from :) ... I have wrapped joeld's answer into a module with global functions that I can use anywhere in my code. ... def enable(): HEADER = '\033[95m' OKBLUE ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › print-colors-python-terminal
Print Colors in Python terminal - GeeksforGeeks
June 20, 2025 - 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.
🌐
Medium
medium.com › @ryan_forrester_ › adding-color-to-python-terminal-output-a-complete-guide-147fcb1c335f
Adding Color to Python Terminal Output: A Complete Guide | by ryan | Medium
January 7, 2025 - These are special sequences that ... Processing...{RESET}") Let’s break down what’s happening here: - `\033` is the escape character - The number between `[` and `m` defines the color - We always use `RESET` after colored ...
Top answer
1 of 2
5

ANSI Escape Codes

ANSI escape codes allow you to do plenty of beautiful things in the terminal. You can change the color of the font, font style (bold, italic, underlined, etc...), and you can also move the cursor to overwrite previous text, to make text change dynamically.

Building codes

Basically, the codes are built like this:

\033[XXXm

where XXX is a series of semicolon-separated parameters.

  • they start with \033[ (or any of the codes specified above + [)
  • then, the contain an arbitrary number of integers separated by ;
  • and they end with m

The code containing only 0 (being \x1B[0m) will reset any style property of the font.

Most of the time, you will print a code changing the style of your terminal, then print a certain string, and then, the reset code.

Here are the codes that you can use to change the color of the font, or the color of the background.

| Color                                  | Font code        | Background code  |
|----------------------------------------|------------------|------------------|
| Black                                  | \x1B[30m         | \x1B[40m         |
| Red                                    | \x1B[31m         | \x1B[41m         |
| Green                                  | \x1B[32m         | \x1B[42m         |
| Yellow                                 | \x1B[33m         | \x1B[43m         |
| Blue                                   | \x1B[34m         | \x1B[44m         |
| Magenta                                | \x1B[35m         | \x1B[45m         |
| Cyan                                   | \x1B[36m         | \x1B[46m         |
| White                                  | \x1B[37m         | \x1B[47m         |
| Any palette color (with V in [0-255])  | \x1B[38;5;Vm     | \x1B[48;5;Vm     |
| Any RGB color (with values in [0-255]) | \x1B[38;2;R;G;Bm | \x1B[48;2;R;G;Bm |
  • List of ANSI color escape sequences
  • Build your own Command-Line with ANSI escape codes
2 of 2
1

You are dealing here with ANSI escape codes.

All follow the pattern \033[XXXm. You can have multiple instructions related to that, not only the one you listed.

For color codes in terminals, it depends of the system. The most basic terminals have a set of 8 different colors:

  • Black: \u001b[30m
  • Red: \u001b[31m
  • Green: \u001b[32m
  • Yellow: \u001b[33m
  • Blue: \u001b[34m
  • Magenta: \u001b[35m
  • Cyan: \u001b[36m
  • White: \u001b[37m
  • Reset: \u001b[0m
🌐
Better Stack
betterstack.com › community › questions › python-how-to-print-colored-text-in-terminal
How do I print colored text to the terminal in Python? | Better Stack Community
January 26, 2023 - You can use ANSI escape codes to print colored text to the terminal in Python. Here is an example of how you can do this: ... def colored_text(color, text): colors = { "red": "\033[91m", "green": "\033[92m", "yellow": "\033[93m", "blue": "\033[94m", "purple": "\033[95m", "cyan": "\033[96m", "white": "\033[97m" } if not color in colors: raise ValueError("Invalid text color") return f"{colors[color]}{text}\033[0m" print(colored_text("red", "This text is red")) print(colored_text("green", "This text is green")) print(colored_text("yellow", "This text is yellow")) print(colored_text("blue", "This text is blue")) print(colored_text("purple", "This text is purple")) print(colored_text("cyan", "This text is cyan")) print(colored_text("white", "This text is white"))
🌐
CodeRivers
coderivers.org › blog › color-codes-for-python
Color Codes in Python: A Comprehensive Guide - CodeRivers
February 22, 2026 - For example, \033[31m sets the text color to red, \033[32m sets it to green, and \033[34m sets it to blue. To reset the color to the default, you can use \033[0m. print("\033[31mThis text is red\033[0m") print("\033[32mThis text is green\033[0m") print("\033[34mThis text is blue\033[0m") Matplotlib ...
Find elsewhere
🌐
Sentry
sentry.io › sentry answers › python › print colored text to terminal with python
Print colored text to terminal with Python | Sentry
ANSI codes to give text-colored backgrounds are also available: BACKGROUND_BLACK = '\033[40m' BACKGROUND_RED = '\033[41m' BACKGROUND_GREEN = '\033[42m' BACKGROUND_YELLOW = '\033[43m' # orange on some systems BACKGROUND_BLUE = '\033[44m' BACKGROUND_MAGENTA = '\033[45m' BACKGROUND_CYAN = '\033[46m' BACKGROUND_LIGHT_GRAY = '\033[47m' BACKGROUND_DARK_GRAY = '\033[100m' BACKGROUND_BRIGHT_RED = '\033[101m' BACKGROUND_BRIGHT_GREEN = '\033[102m' BACKGROUND_BRIGHT_YELLOW = '\033[103m' BACKGROUND_BRIGHT_BLUE = '\033[104m' BACKGROUND_BRIGHT_MAGENTA = '\033[105m' BACKGROUND_BRIGHT_CYAN = '\033[106m' BACKGROUND_WHITE = '\033[107m' Codes can be combined for multiple effects: print(GREEN + BACKGROUND_RED + "green on red" + RESET) Many additional colors can be accessed using 8-bit color codes.
🌐
PyLand
pylandschool.com › en › blog › article › ansi-color-table
ANSI Color Table for Python 🎨 - PyLand Blog
July 13, 2026 - The \033 sequence represents the special Escape character. Always add RESET after colored text, or later output may remain colored. Black and white depend on the terminal theme. For example, black text may be difficult to see on a dark background.
🌐
ozzmaker.com
ozzmaker.com › add-colour-to-text-in-python
Add Colour to Text in Python | ozzmaker.com
July 25, 2021 - It does not work on mine, I use 3.9.0 and tried both \x1b and \033, only the start got hided and other got displayed, so the color did not display. ... Same here. Any solution ? ... For python three the escape code is “\x1b[” then you add stuff so bright green would be “\x1b[1;32m” I hope that helped ya out.
🌐
GitHub
gist.github.com › nazwadi › ca00352cd0d20b640efd
Bash Colors in Python · GitHub
Bash Colors in Python · Raw · python_bash_font · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters · Show hidden characters · Copy link · Copy Markdown · \033[91m ·
🌐
Sololearn
sololearn.com › en › Discuss › 2795745 › why-this-python-color-codes-are-not-working-in-windows-os
why this python color codes are not working in windows os? | Sololearn: Learn to code for FREE!
class bcolors: GREEN = '\033[92m' YELLOW = '\033[93m' RED = '\033[91m' PINK = '\033[95m' BLUE = '\033[94m' BOLD = '\033[1m' ENDC = '\033[0m' print(bcolors.BLUE+"Hello World") # working fine in Linux terminal · pythonpython3 · 26th May 2021, 3:13 PM · Ratnapal Shende · 3 Answers · Answer · + 1 · It's working fine on pycharm. What do you mean by windows? This first answer to this question might help you: https://stackoverflow.com/questions/287871/how-to-print-colored-text-to-the-terminal Okay this might help you: https://stackoverflow.com/questions/36760127/how-to-use-the-new-support-for-ansi-escape-sequences-in-the-windows-10-console ·
🌐
Go Packages
pkg.go.dev › github.com › whitedevops › colors
colors package - github.com/whitedevops/colors - Go Packages
Package colors provides the list of standard terminal color codes. Constants · View Source · const ( ResetAll = "\033[0m" Bold = "\033[1m" Dim = "\033[2m" Underlined = "\033[4m" Blink = "\033[5m" Reverse = "\033[7m" Hidden = "\033[8m" ResetBold = "\033[21m" ResetDim = "\033[22m" ResetUnderlined = "\033[24m" ResetBlink = "\033[25m" ResetReverse = "\033[27m" ResetHidden = "\033[28m" Default = "\033[39m" Black = "\033[30m" Red = "\033[31m" Green = "\033[32m" Yellow = "\033[33m" Blue = "\033[34m" Magenta = "\033[35m" Cyan = "\033[36m" LightGray = "\033[37m" DarkGray = "\033[90m" LightRed = "\033
Top answer
1 of 2
38

You can use ASCII color codes at the beginning of your print to change the color, as an example '\033[91m' for RED and '\033[94m' for BLUE.

e.g

if array[0][i] > 7:
   print '\033[94m' + oreName[i]+"=   %.3f \t 5"%(array[0][i])
elif array[0][i] < 7:
   print '\033[91m' + oreName[i]+"=   %.3f \t 5"%(array[0][i])

You can read about ASCII escape codes here.

Edit: Added additional example code.

Here is a list of some common colors you can use:

Red = '\033[91m'
Green = '\033[92m'
Blue = '\033[94m'
Cyan = '\033[96m'
White = '\033[97m'
Yellow = '\033[93m'
Magenta = '\033[95m'
Grey = '\033[90m'
Black = '\033[90m'
Default = '\033[99m'

Additionally as mentioned in the comment. You can chain these to get different colors on the same line.

print '\033[91m' + 'Red' + "\033[99m" + 'Normal' + '\033[94m' + 'Blue

You could even do a function.

# Store a dictionary of colors.
COLOR = {
    'blue': '\033[94m',
    'default': '\033[99m',
    'grey': '\033[90m',
    'yellow': '\033[93m',
    'black': '\033[90m',
    'cyan': '\033[96m',
    'green': '\033[92m',
    'magenta': '\033[95m',
    'white': '\033[97m',
    'red': '\033[91m'
}


def print_with_color(message, color='red'):
    print(COLOR.get(color.lower(), COLOR['default']) + message)


print_with_color('hello colorful world!', 'magenta')
print_with_color('hello colorful world!', 'blue')
2 of 2
0

for Easy copy-paste after print:

for style in range(8):
    for fg in range(30,38):
        s1 = ''
        for bg in range(40,48):
            myform = ';'.join([str(style), str(fg), str(bg)])
            skel = '\x1b[{};{};{}m'.format(myform.split(';')[0],myform.split(';')[1],myform.split(';')[2])
            s1 += f'\x1b[{myform}m {repr(skel)} \x1b[0m'
        print(s1)
    print('\n')

This code print multiple pretty lines with different colors. example: '\x1b[5;31;40m' '\x1b[5;31;41m' '\x1b[5;31;42m' '\x1b[5;31;43m' for easy copy-paste to code. After print add to you code:

mycolor = '\x1b[5;35;43m'
rest    = '\x1b[0m'

and print for preview:

print(f"{mycolor}Hello world!{rest}")

for preview: Stack attach

🌐
Fabian Lee
fabianlee.org › 2021 › 12 › 03 › python-printing-in-color-using-ansi-color-codes
Python: printing in color using ANSI color codes | Fabian Lee : Software Engineer
December 5, 2021 - Although there are Python modules [1,2,3] specially suited for displaying text to the console in color, if you want a quick no-dependency method then you can use ANSI color codes directly. Here is an example of printing a line in green, then red. print("\033[0;32mOK this is green\033[00m") print("\033[0;31mERROR this is red\033[00m") Additional color codes can be found here and here.
🌐
Jakob-bagterp
jakob-bagterp.github.io › colorist-for-python › ansi-escape-codes › introduction
Ultimate Guide to ANSI Escape Codes - Colorist for Python 🌈
Supported by most modern operating systems, including Windows, macOS, and Linux, they allow you to add colors and effects to your terminal output. ... % I want RED, GREEN, BLUE text... % ... or RED, GREEN, BLUE background... % ... and I want BLINKING text. They always start with \x1b, \033, \u001b, or \e or depending on the operating system or programming language.
Top answer
1 of 16
3045

This somewhat depends on what platform you are on. The most common way to do this is by printing ANSI escape sequences. For a simple example, here's some Python code from the Blender build scripts:

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKCYAN = '\033[96m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

To use code like this, you can do something like:

print(bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC)

Or, with Python 3.6+:

print(f"{bcolors.WARNING}Warning: No active frommets remain. Continue?{bcolors.ENDC}")

This will work on unixes including OS X, Linux and Windows (provided you use ANSICON, or in Windows 10 provided you enable VT100 emulation). There are ANSI codes for setting the color, moving the cursor, and more.

If you are going to get complicated with this (and it sounds like you are if you are writing a game), you should look into the "curses" module, which handles a lot of the complicated parts of this for you. The Python Curses HowTO is a good introduction.

If you are not using extended ASCII (i.e., not on a PC), you are stuck with the ASCII characters below 127, and '#' or '@' is probably your best bet for a block. If you can ensure your terminal is using a IBM extended ASCII character set, you have many more options. Characters 176, 177, 178 and 219 are the "block characters".

Some modern text-based programs, such as "Dwarf Fortress", emulate text mode in a graphical mode, and use images of the classic PC font. You can find some of these bitmaps that you can use on the Dwarf Fortress Wiki see (user-made tilesets).

The Text Mode Demo Contest has more resources for doing graphics in text mode.

2 of 16
1220

There is also the Python termcolor module. Usage is pretty simple:

from termcolor import colored

print(colored('hello', 'red'), colored('world', 'green'))

It may not be sophisticated enough, however, for game programming and the "colored blocks" that you want to do...

To get the ANSI codes working on windows, first run

os.system('color')
🌐
Stack Abuse
stackabuse.com › how-to-print-colored-text-in-python
How to Print Colored Text in Python
February 27, 2023 - The \033[0;0m code represents a reset pattern that returns the terminal back to its original color scheme. This will provide the following output: Looks much better. Colorama is a Python package that provides methods to print colored text in Python. It only supports the 16-colors scheme.
🌐
GitHub
github.com › optio50 › PythonColors › blob › main › color-test.py
PythonColors/color-test.py at main · optio50/PythonColors
#!/usr/bin/python3 · · · # colored text and background · """Colors class: reset all colors with colors.reset · two sub classes · fg for foreground · bg for background · use as colors.subclass.colorname. i.e. colors.fg.red or colors.bg.green · also, the generic bold, disable, underline, reverse, strike through, and invisible · work with the main class i.e. colors.bold""" · class colors: reset = '\033[0m' bold = '\033[01m' disable = '\033[02m' underline = '\033[04m' reverse = '\033[07m' strikethrough = '\033[09m' invisible = '\033[08m' ·
Author: optio50