Skip to content

How to Customize Text Styling and Colors#

Instead of printing statements directly to the terminal, you can also customize and combine various colors and styling to text strings before printing them. This gives you more flexibility to create complex patterns with different styles.

How it Works#

When using the style_text() method to apply colors and styling to a text string...

Python
1
2
3
from colorist import style_text, Color

text = style_text("APPROVED", Color.GREEN)

... this wraps the text in the relevant ANSI escape codes, e.g. \033[32mAPPROVED\033[0m.

If you print the newly styled text...

4
print(text)

... it appears like this in the terminal:

% APPROVED

Examples#

Single Style#

In addition to standard colors for text and background, you can also use effects as well as extended colors for the VGA, RGB, HSL, and Hex palettes.

For example:

Python
1
2
3
4
from colorist import style_text, BgColorRGB

text = style_text("I want BURGUNDY background", BgColorRGB(128, 0, 32))
print(text)

How it appears in the terminal:

% I want BURGUNDY background

Disclaimer

Not all terminals support 8-bit VGA colors or 24-bit colors in RGB, HSL, or Hex. If your terminal does support such advanced colors, read on.

Multiple Styles#

You can also combine several styles by adding multiple styling arguments to the style_text() method:

Python
1
2
3
4
from colorist import style_text, Color, Effect

text = style_text("WARNING", Color.YELLOW, Effect.BOLD, Effect.BLINK)
print(text)

How it appears in the terminal:

% WARNING