Python, Color Text

Print Color Text with Python

A step-by-step approach to creating stunning color text in Python

Davide Gazzè - Ph.D.
syntax-error
Published in
4 min readJan 16, 2023

--

Photo by Rodion Kutsaiev on Unsplash

Hi everyone,

one of the most interesting things when I started using a computer, a Commodore 64, was always the ability to write colored text. It may sound childish, and indeed I was a child, but at the time the ability to display colors at my leisure was always something that filled me with joy. Of course, age and technological advancement have led me to consider different aspects of computing cool. But sometimes coming back as a child gives a little help to move forward. Therefore, in this post, the possibility of printing colored text in python will be discussed.

Introduction

On Linux, it is possible to print colored text using some `ANSI escape sequences [1], for example, if you open your terminal, and type:

echo -e "\e[31mColored Text\e[0m"

and you will see the following:

The text is now Red. Entering in details, we can analyze the pattern:

echo -e "\e[COLORmSample Text\e[0m"

this string is formed by different parameters:

  • -e: Enable interpretation of backslash escapes

--

--