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.

Answer from joeld on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › print-colors-python-terminal
Print Colors in Python terminal - GeeksforGeeks
June 20, 2025 - 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. ... 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')
🌐
Medium
vascosim.medium.com › pythonic-aesthetics-enhancing-terminal-readability-with-color-52f6244e2e30
How to Print Colored Text in Python | Medium
January 23, 2024 - # Print text in red print('\033[31mHello World but Red!\033[0m') # Print text with a blue background print('\033[44mHello World but on Blue!\033[0m') # Print text in red with a blue background print('\033[31m\033[44mHello World but Red on ...
Discussions

colored printing in python
🌐 r/learnpython
5
9
February 8, 2022
How do I print colored output to the terminal in Python? - Stack Overflow
Would the Python termcolor module do? This would be a rough equivalent for some uses. from termcolor import colored print colored('hello', 'red'), colored('world', 'green') The example is right from this post, which has a lot more. Here is a part of the example from docs · import sys from termcolor import colored, cprint text ... More on stackoverflow.com
🌐 stackoverflow.com
March 8, 2020
How To Use Colors in Terminal (Python)
https://github.com/tartley/colorama More on reddit.com
🌐 r/vscode
9
9
June 27, 2019
Printing Colored Text in Console
It depends on your OS. If you're on Windows, try colorama (it also works on OSX and Linux, but doesn't support bold/italic text). On Linux/OSX, try termcolor . Edit: also, post questions in r/learnpython next time. More on reddit.com
🌐 r/Python
5
3
July 11, 2017
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')
🌐
Reddit
reddit.com › r/learnpython › colored printing in python
r/learnpython on Reddit: colored printing in python
February 8, 2022 -

Hi, someone has a way to color print in python? Thanks in advance

🌐
Stack Abuse
stackabuse.com › how-to-print-colored-text-in-python
How to Print Colored Text in Python
February 27, 2023 - We first import the functions: init() (to initialize the module and to set the autoreset to True so we don't have to reset it manually), Fore (Foreground text object), Back (Background Object) and Style (Style Object). Each object has its own set of constants that can be called in the print() function. By appending these components in a human-friendly way, Colorama converts values like YELLOW to 43, for the Back object, RED to 31 for the Fore object, etc.
🌐
Sentry
sentry.io › sentry answers › python › print colored text to terminal with python
Print colored text to terminal with Python | Sentry
How can I print colored text to the terminal in Python? On most platforms, terminal colors are produced using ANSI escape sequences. We can use these directly, or through a Python library. Terminal output can be colored by surrounding it with ANSI escape sequences: BLACK = '\033[30m' RED = '\033[31m' GREEN = '\033[32m' YELLOW = '\033[33m' # orange on some systems BLUE = '\033[34m' MAGENTA = '\033[35m' CYAN = '\033[36m' LIGHT_GRAY = '\033[37m' DARK_GRAY = '\033[90m' BRIGHT_RED = '\033[91m' BRIGHT_GREEN = '\033[92m' BRIGHT_YELLOW = '\033[93m' BRIGHT_BLUE = '\033[94m' BRIGHT_MAGENTA = '\033[95m'
Top answer
1 of 9
128

Would the Python termcolor module do? This would be a rough equivalent for some uses.

from termcolor import colored

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

The example is right from this post, which has a lot more. Here is a part of the example from docs

import sys
from termcolor import colored, cprint

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

A specific requirement was to set the color, and presumably other terminal attributes, so that all following prints are that way. While I stated in the original post that this is possible with this module I now don't think so. See the last section for a way to do that.

However, most of the time we print short segments of text in color, a line or two. So the interface in these examples may be a better fit than to 'turn on' a color, print, and then turn it off. (Like in the Perl example shown.) Perhaphs you can add optional argument(s) to your print function for coloring the output as well, and in the function use module's functions to color the text. This also makes it easier to resolve occasional conflicts between formatting and coloring. Just a thought.


Here is a basic approach to set the terminal so that all following prints are rendered with a given color, attributes, or mode.

Once an appropriate ANSI sequence is sent to the terminal, all following text is rendered that way. Thus if we want all text printed to this terminal in the future to be bright/bold red, print ESC[ followed by the codes for "bright" attribute (1) and red color (31), followed by m

# print "\033[1;31m"   # this would emit a new line as well
import sys
sys.stdout.write("\033[1;31m")
print "All following prints will be red ..."

To turn off any previously set attributes use 0 for attribute, \033[0;35m (magenta).

To suppress a new line in python 3 use print('...', end=""). The rest of the job is about packaging this for modular use (and for easier digestion).

File colors.py

RED   = "\033[1;31m"  
BLUE  = "\033[1;34m"
CYAN  = "\033[1;36m"
GREEN = "\033[0;32m"
RESET = "\033[0;0m"
BOLD    = "\033[;1m"
REVERSE = "\033[;7m"

I recommend a quick read through some references on codes. Colors and attributes can be combined and one can put together a nice list in this package. A script

import sys
from colors import *

sys.stdout.write(RED)
print "All following prints rendered in red, until changed"

sys.stdout.write(REVERSE + CYAN)
print "From now on change to cyan, in reverse mode"
print "NOTE: 'CYAN + REVERSE' wouldn't work"

sys.stdout.write(RESET)
print "'REVERSE' and similar modes need be reset explicitly"
print "For color alone this is not needed; just change to new color"
print "All normal prints after 'RESET' above."

If the constant use of sys.stdout.write() is a bother it can be be wrapped in a tiny function, or the package turned into a class with methods that set terminal behavior (print ANSI codes).

Some of the above is more of a suggestion to look it up, like combining reverse mode and color. (This is available in the Perl module used in the question, and is also sensitive to order and similar.)


A convenient list of escape codes is surprisingly hard to find, while there are many references on terminal behavior and how to control it. The Wiki page on ANSI escape codes has all information but requires a little work to bring it together. Pages on Bash prompt have a lot of specific useful information. Here is another page with straight tables of codes. There is much more out there.

This can be used alongside a module like termocolor.

2 of 9
28

I suggest sty. It's similar to colorama, but less verbose and it supports 8bit and 24bit colors. You can also extend the color register with your own colors.

Examples:

from sty import fg, bg, ef, rs

foo = fg.red + 'This is red text!' + fg.rs
bar = bg.blue + 'This has a blue background!' + bg.rs
baz = ef.italic + 'This is italic text' + rs.italic
qux = fg(201) + 'This is pink text using 8bit colors' + fg.rs
qui = fg(255, 10, 10) + 'This is red text using 24bit colors.' + fg.rs

# Add custom colors:

from sty import Style, RgbFg

fg.orange = Style(RgbFg(255, 150, 50))

buf = fg.orange + 'Yay, Im orange.' + fg.rs

print(foo, bar, baz, qux, qui, buf, sep='\n')

Demo:

Find elsewhere
🌐
Vultr
docs.vultr.com › python › examples › print colored text to the terminal
Python Program to print colored text to the terminal
December 26, 2024 - Use the following command: ... Import the colored function from the termcolor module. Apply the colored function to the text you wish to print, specifying the desired color as an argument.
🌐
ozzmaker.com
ozzmaker.com › add-colour-to-text-in-python
Add Colour to Text in Python | ozzmaker.com
July 25, 2021 - I just want Python code to display text in a specified colour. All help appreciated 🙂 ... Same for me. I am using Spyder (Python 3.7) and none of the ideas are working out. I have tired colorama, termcolor, ANSI escape code method and others too. Can someone please help me out? ... You can use ‘ pip install colorama ‘ then in the script : “` from colorama import Fore from colorama import Style #to print red text print(f'{Fore.RED} hi there {Style.RESET_ALL}’) #style reset all “`
🌐
CodeRivers
coderivers.org › blog › print-with-colors-python
Printing with Colors in Python - CodeRivers
February 22, 2026 - import os from colorama import init if os.name == 'nt': init() print("\033[31mThis text should be red on both Windows and Unix - like systems\033[0m") While it can be tempting to use a wide variety of colors, it's important to keep the color scheme simple. Using too many colors or colors that are difficult to read can make the output less useful. Stick to a few colors that have clear meanings, such as using green for success messages, red for error messages, and yellow for warnings. Printing with colors in Python can enhance the visual appeal and usability of your console applications.
🌐
Javatpoint
javatpoint.com › how-to-print-colored-text-in-python
How to Print Colored Text in Python - Javatpoint
How to Print Colored Text in Python with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.
🌐
AskPython
askpython.com › python › examples › print-colored-text-to-the-terminal-in-python
Print Colored Text to the Terminal in Python - AskPython
February 23, 2023 - In termcolor and colorama, we have to import the library to use its functionalities, whereas ANSI is quick and requires no installation. These libraries representing data with particular text colors can be helpful. Red could be used for warnings, blue for the right input, and so on, according to the requirement. You may also check: Python Pretty print JSON – A Complete Guide
🌐
Medium
medium.com › syntaxerrorpub › print-color-text-with-python-a0d10845596f
Print Color Text with Python | by Davide Gazzè - Ph.D.
March 31, 2023 - Therefore, in this post, the possibility of printing colored text in python will be discussed. On Linux, it is possible to print colored text using some `ANSI escape sequences [1], for example, if you open your terminal, and type: ... The text ...
🌐
Google Colab
colab.research.google.com › drive › 1NEPTrzJux2wySxTsCL2oT907FsmHyLAa
Print color text with Python - Colab
Collecting colorama Downloading colorama-0.4.6-py2.py3-none-any.whl.metadata (17 kB) Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB) Installing collected packages: colorama Successfully installed colorama-0.4.6 · spark Gemini · import colorama · from colorama import Fore · print(Fore.RED + 'This text is red in color') This text is red in color ·
🌐
GitHub
github.com › AidinHamedi › Python-color-print-V2
GitHub - AidinHamedi/Python-color-print-V2: A simple and user friendly way to print colored text to the console. · GitHub
Default is '>'. Examples: ~~~python print_Color('Hello, World!') # Prints 'Hello, World!' in normal color. print_Color('<red>Hello in red<green> Hello in green') # Prints 'Hello in red' in red and 'Hello in green' in green.
Author: AidinHamedi
🌐
YouTube
youtube.com › finxter
How to Print Colored Text in Python - YouTube
Full Tutorial: https://blog.finxter.com/how-to-print-colored-text-in-pythonEmail Academy: https://blog.finxter.com/email-academy/►► Do you want to thrive as ...
Published: May 17, 2022
Views: 1K
🌐
Linux find Examples
queirozf.com › entries › add-colour-to-text-on-the-terminal-examples-with-bash-and-python
Add Colour to Text on the Terminal: Examples with Bash and Python
April 24, 2022 - Red text · Yellow text · Bold ... you create (php, ruby, perl, etc), not just bash or Python. See full color codes here · Wrap your string with "\033[31m" and "\033[00m": Example: Print "ERROR" in red ·...
🌐
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", ...