mar tin mar tin - 2 months ago 9
Python Question

Python: printing in color and with style

If I want to print colored text, I would just do, using codes

green = '\033[0;32m'
print green + 'Hello'


and that gives me green text. If I want to have bold text, I would just use the code for bold, which is
'\033[1m'


Now, I tried to combine them as

print bold + green + 'Hello'


where
bold
is the aforementioned code, and that didn't work, gave me just green text with no bold style.

Anyway, what am I missing to combine color with style?

Answer

I can recommand this gist by Diego Navarro Mellén.

You can combine whatever you like when doing something like this:

with pretty_output(BOLD, FG_GREEN) as out:
    out.write('This is a bold text in green')
Comments