colorize(color=None, string=None, doprint=None, output=False, enable=True, showhelp=False, fg=None, bg=None, style=None)[source]#

Colorize output text.

  • color (str) – the color you want (use ‘bg’ with background colors, e.g. ‘bgblue’); alternatively, use fg, bg, and style

  • string (str) – the text to be colored

  • doprint (bool) – whether to print the string (default true unless output)

  • output (bool) – whether to return the modified version of the string (default false)

  • enable (bool) – switch to allow sc.colorize() to be easily turned off without converting to a print() statement

  • showhelp (bool) – show help rather than changing colors

  • fg (str) – foreground colour

  • bg (str) – background colour

  • style (str) – font style (eg, italic, underline, bold)


sc.colorize('green', 'hi') # Simple example
sc.colorize(['yellow', 'bgblack']); print('Hello world'); print('Goodbye world'); colorize() # Colorize all output in between
bluearray = sc.colorize(color='blue', string=str(range(5)), output=True); print("c'est bleu: " + bluearray)
sc.colorize('magenta') # Now type in magenta for a while
sc.colorize() # Stop typing in magenta
sc.colorize('cat in the hat', fg='#ffa044', bg='blue', style='italic+underline') # Alternate usage example

To get available colors, type sc.colorize(showhelp=True).

New in version 1.3.1: “doprint” argument; ansicolors shortcut