[Edu-sig] Programming for the fun of it

Guido van Rossum guido@python.org
Thu, 14 Dec 2000 11:05:36 -0500


> Hi everybody, I just joined the list.  I am fairly new at python programming 
> and I'm taking a course for it in Yorktown High School With Jeff Elkner (one 
> of the people in this list) teaching it.  Python seems like an interesting 
> language, although C++ seems much more powerful.  I have a question to which 
> I could not find the answer anywhere.  How would you be able to change the 
> text color for text in python? What is the code.
> -Thanks
> -Teddy

Hi Teddy,

I saw that you alread got advice to use curses.  That's a very
complicated thing though!  Here's a simpler approach that *may* work
for you.  It's more fun because it lets you experiment a little bit.

For terminal windows, the escape codes for generating color are pretty
much standardized.  The most-used protocol was first used by the very
successful VT-100 terminal many years ago, and while you can't buy
those any more, their way of encoding terminal magic is still used by
most terminal programs.  Try this program:

def color_on(i, j):
    return "\033[%d;%dm" % (i, j)

def color_off():
    return "\033[00m"

i = 1
for j in range(128):
    print color_on(i, j) + "color %d" % j + color_off()

For me, this prints color 0 in black, most other colors in blue, a
series of different colors starting at 30, another series of different
colored backgrounds starting at 40, and more colors starting at 90.

You can also experiment with other values for i -- it has some cool
background effects.

Oh, one more thing.  I did a Google search on VT100 escape sequences
and found a useful summary: http://www.termsys.demon.co.uk/vtansi.htm

Good luck!

--Guido van Rossum (home page: http://www.python.org/~guido/)