Simple way to add color in python?


#1

hello there, i was wondering if there is a simple way of adding color in python. My current ways is to use the making it the console display it as if it was a comment/sting/keyword. so if people have modified their “highlighting” then it will be different . code:

import sys
shell = sys.stdout.shell
shell.write("Printed as it was a comment in IDLE","COMMENT")
shell.write("Printed as it was a String in IDLE","STRING")
shell.write("Printed as it was a Keyword in IDLE","KEYWORD")


#2

colorama probably does what you’re looking for
install it with pip
see colorama’s git repository for examples

Note that it’s up to whatever gui you have running to actually display the colours, so it’s very much platform dependant.

The usual terminals you’ll find on unixy systems (linux, mac, others) do colors by printing certain strings, for those it would be fairly easy to roll your own solution (you’d write a function which adds the green at the start and reset to default at the end) … there are probably terminals for windows who pay attention to those strings


#3

thank you for your input .I am looking forward to using colorama :slight_smile: