Python: printing in color using ANSI color codes

python-logoAlthough there are Python modules [1,2,3] specially suited for displaying text to the console in color, if you want a quick no-dependency method then you can use ANSI color codes directly.

Here is an example of printing a line in green, then red.

print("\033[0;32mOK this is green\033[00m")

print("\033[0;31mERROR this is red\033[00m")

Additional color codes can be found here and here. I’ve uploaded a full example to github.

REFERENCES

studytonight.com, printing colored text using ascii

stackabuse.com, printing color with python

skillsugar, printing color with python

github, ASCI color codes

github example code

stackoverflow, list of foreground colors

fabianlee.org, printing in color via BASH