Getting started with colorsΒΆ

To highlight using colors:

from ansicolor import cyan
from ansicolor import green
from ansicolor import red
from ansicolor import white

print("Let's try two colors: %s and %s!" % (red("red"), green("green")))
print("It's also easy to produce text in %s," % (red("bold", bold=True)))
print("...%s," % (green("reverse", reverse=True)))
print("...and %s." % (cyan("bold and reverse", bold=True, reverse=True)))
../_images/getting_started_1.png

This will emit ansi escapes into the string: one when starting a color, another to reset the color back to the default:

>>> from ansicolor import green

>>> green("green")
'\x1b[0;0;32mgreen\x1b[0;0m'

If I want to be able to pass a color as an argument I can also use the colorize function:

from ansicolor import Colors
from ansicolor import colorize

print(colorize("I'm blue", Colors.Blue))
../_images/getting_started_2.png

I can also apply color on a portion of a string:

from ansicolor import Colors
from ansicolor import colorize

print(colorize('''"I'm blue", said the smurf.''', Colors.Blue, start=1, end=9))
../_images/getting_started_3.png