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...
Discussions

How do I print colored output to the terminal in Python? - Stack Overflow
Is there a Python equivalent of Perl's print color 'red'; print ; print color 'reset'; available? I am using this approach: "\x1b[1;%dm" % ( ) + "E... More on stackoverflow.com
๐ŸŒ stackoverflow.com
A Python module for terminal colors.
This is my module, thanks i really didnt expected this to happen More on reddit.com
๐ŸŒ r/Python
12
25
July 23, 2023
python - How do I print colored text to the terminal? - Stack Overflow
How do I output colored text to the terminal in Python? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Color a list or a string
Hi everyone, Is there a way to color a string or a list/tuple ? for example something that we can do is the following: H = 'hello' res = '' for i in range(len(H)): res += f'\033[0;33m{H[i]}' print(res) This will print the word hello in yellow. However if we try to return it as a string then ... More on discuss.python.org
๐ŸŒ discuss.python.org
11
0
April 2, 2023
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:

๐ŸŒ
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.
๐ŸŒ
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 ยท
๐ŸŒ
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.
๐ŸŒ
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
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')
๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ print colored text to terminal with python
Print colored text to terminal with Python | Sentry
On most platforms, terminal colors are produced using ANSI escape sequences. We can use these directly, or through a Python library.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Color a list or a string - Python Help - Discussions on Python.org
April 2, 2023 - Hi everyone, Is there a way to color a string or a list/tuple ? for example something that we can do is the following: H = 'hello' res = '' for i in range(len(H)): res += f'\033[0;33m{H[i]}' print(res) This will โ€ฆ
๐ŸŒ
Iws
fwd.iws.edu โ€บ en โ€บ python-print-colored.html
Python Print Colored - Printable Study Planner
March 16, 2024 - Python Print Colored Web to print coloured text on the terminal in python, you can use the colored () function from the termcolor package..
๐ŸŒ
Medium
medium.com โ€บ ai-does-it-better โ€บ print-colored-text-in-python-enhance-terminal-output-b90aede058c8
Print Colored Text in Python โ€” Using colorama-termcolor | by Doug Creates | AI Does It Better | Medium
December 28, 2024 - Printing colored text in Python involves using third-party libraries like colorama to manipulate terminal output, enabling developers to enhance readability and differentiate information through colors.
๐ŸŒ
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

๐ŸŒ
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.