Output in color

Alex Martelli aleax at aleax.it
Fri Sep 5 06:39:37 EDT 2003


Bradford Powers wrote:

> I'm curious how I can make Python print text in color.

That depends strictly on your printer.  With my hp LaserJet 1200,
no way -- not even Python's power can overcome the hardware's
limitations in this regard... it's a black-and-white printer and
that's all there is to it!  If I did have a color printer, then
I would have Python produce the appropriate postscript code, or
"escape-sequences" in whatever printer-specific language a given
printer requires to have it output color text (or, depending on my
operating system, printer driver, filters, etc, I might have to
send appropriate "escape-sequences" or whatever to the DRIVER in
order to convince it to drive the printer appropriately).

Much the same holds if you're using "printing" in the vague
sense in which Python itself uses it in the keyword "print"
(which I detest and consider a wart, albeit shared by such
diverse languages as Python, Perl, C, Fortran, etc): it all
depends on what device (physical hardware one, or one in some
part simulated by software) you're outputting to.

For example,

print '\033[1;31mciao!'

will set all text emitted henceforward to red _IF_ the
terminal (or emulator thereof) on which the text is being
written is ANSI compliant.


Alex





More information about the Python-list mailing list