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.

Answer from zdim on Stack Overflow
🌐
PyPI
pypi.org › project › colored
colored · PyPI
Simple python library for color and formatting to terminal
🌐
Google Sites
sites.google.com › view › paztronomer › blog › basic › python-colors
paztronomer - python colors
Fun fact: What matplotlib does is to transform a set of 3 or 4 numbers to a color (transforming these numbers to a range between 0 and 1, think of it as a percentage where 1 is 100%) When 3 numbers are used, the result is a color that is a combination of red, blue, and green...
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:

🌐
Matplotlib
matplotlib.org › stable › gallery › color › named_colors.html
List of named colors — Matplotlib 3.11.2 documentation
Download Jupyter notebook: named_colors.ipynb · Download Python source code: named_colors.py · Download zipped: named_colors.zip · Gallery generated by Sphinx-Gallery ·
🌐
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.
🌐
Reddit
reddit.com › r/python › a python module for terminal colors.
r/Python on Reddit: A Python module for terminal colors.
July 23, 2023 -

Hey, r/Python community!

I wanted to share an amazing discovery I made recently. There's a fantastic module out there for adding colors to your terminal in Python!

This module, called Chromify, offers a wide range of options to bring your terminal to life with vibrant colors. You can play around with different color formats like RGB, hex, hsl, hsv, CMYK, and CSS. It even includes over 200 predefined colors that you can easily use.

Chromify also provides useful additional classes and functions like color conversion, horizontal and vertical gradients, cursor control, styles, and special characters.

It's incredible how easy it is to use Chromify in your Python projects and elevate the visual experience of the terminal. I encourage you to give it a try and explore all the possibilities it offers!

You can find Chromify on PyPi, and its repository on GitHub for more information and to start experimenting with colors in your terminal. I'm sure you'll love it!

I hope this information is helpful to the r/Python community. Have fun exploring Chromify and adding a special touch to your Python projects with terminal colors!

Good luck and happy coding! 🚀

- PyFinder -- PyPi AI Finder 🤖
Source: https://pypi.org/project/Chromify/ -- pip install Chromify

Find elsewhere
🌐
Stack Abuse
stackabuse.com › how-to-print-colored-text-in-python
How to Print Colored Text in Python
February 27, 2023 - There's a placeholder to determine whether the color will be applied to the text or the background; 38;5; is for the text and 48;5; is for the background. This is followed by the color code ranging from 0 to 255. Based on the above syntax, let's try recreating the StackAbuse logo in Python using an ANSI Escape Sequence.
Top answer
1 of 16
3044

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')
🌐
YouTube
youtube.com › watch
Colored Console Output in Python - YouTube
In this video we learn how to print colored console output in Python.◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾📚 Programming Books & Merch 📚💻 The Algorithm Bible Book: https://www...
Published: May 9, 2021
🌐
YouTube
youtube.com › tech with tim
How To Print Colored Text in Python (Colorama Tutorial) - YouTube
In this video I'll be showing how to print colored text in python. This method will show you how to output colored text from python in windows, mac and linux...
Published: December 21, 2020
Views: 128K
🌐
Michael Currin
michaelcurrin.github.io › dev-cheatsheets › cheatsheets › python › libraries › colors.html
Colors | Dev Cheatsheets
from colors import red, green, blue print(red('This is red')) print(green('This is green')) print(blue('This is blue')) print(red('red on blue', bg='blue')) print(green('green on black', bg='black', style='underline'))
🌐
GitHub
github.com › timofurrer › colorful
GitHub - timofurrer/colorful: Terminal string styling done right, in Python :tada: · GitHub
colorful's Python API is based on color names like in cf.bold_white_on_black('Hello'). During runtime these color names are translated into proper ANSI escape code sequences supported by the color mode in use. However, all color names are registered in a color palette which is basically a mapping between the color names and it's corresponding RGB value.
Author: timofurrer
🌐
PyPI
pypi.org › project › termcolor
termcolor · PyPI
Calling colored or cprint with a truthy force_color forces colour.
🌐
PyPI
pypi.org › project › print-color
print-color · PyPI
Print Color is a minimalist approach to terminal color printing in Python.
🌐
Instructables
instructables.com › design › software
Printing Colored Text in Python Without Any Module : 3 Steps - Instructables
October 22, 2018 - We see that after the green text is printed, the whole shell changes color! ... It turns all the text white...and a different type of 'dull' white if you see it carefully. If you want to commit your code to GITHUB, then many people using your code may have a customized Python Shell with probably YELLOW or Something as their text/background color!
🌐
PyPI
pypi.org › project › colour
colour
JavaScript is disabled in your browser. Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
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 - There are Python modules especially suited for displaying colored text, ANSI is a no-dependency method used for color codes. ANSI escape sequence is in-band signaling to control locations of cursors and colors, to style the font, and much more.
🌐
Towards Data Science
towardsdatascience.com › home › data science › python - print colored text with ease
Python - Print colored text with ease | Towards Data Science
October 18, 2021 - Py-console is a package that I've created that makes printing colors super-easy. It emulates JavaScripts well-known methods likeconsole.log, console.warn, console.error etc. It also adds some more functionalities. Want to create your own Python package? → check out [this article](https://mikehuls.medium.com/create-your-custom-python-package-that-you-can-pip-install-from-your-git-repository-f90465867893) for instructions.