[Tutor] More about ANSI color

Kent Johnson kent_johnson at skillsoft.com
Fri Sep 17 12:02:03 CEST 2004


Why not make a function to convert the tokens you want to use to the actual 
tokens and another function that converts and prints? Then you could say
colorPrint('{RHello world{X')

colorPrint would be
def colorPrint(s):
   print colorize(s)

colorize() could be based on this Python Cookbook recipe which does 
multiple replacements in a string with a single call to re.sub(): 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330

Put the functions into a module and you can import them and use them 
wherever you like.

Kent

At 10:24 PM 9/16/2004 -0400, Kevin wrote:
>I am trying to figure out if this can be done or not. As of right now the 
>way I add color to a string is like this
>
>fRed = chr(27) + '[31m'
>bOff = chr(27) + '[22m'  #Turn bold off
>cEnd = chr(27) + '[0m'  #Reset the color back to normal
>
>print fRed+'Hello world'+cEnd
>#turn bold off would be
>print bOff+fRed+'Hello world'+cEnd
>
>Is there a way to make it so that there is less typing for example you 
>could mabey right the "Hello world" like this
>
>print '{RHello world{X'
>
>where {R is red or {r would be unbold red and {X is reset.
>Thanks
>
>Kevin
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list