Styling text in Python
Disclaimer: Python 3 only and console only
Using Colorama
one of the simplest ways I've found of colouring and styling my text in python is by using Colorama
import colorama
It has 3 different attributes
Fore
- colour (of text)
Back
- background
Style
- things like Bold and italics
when styling code remember after styling the piece of text you must end the styling, to do this in colorama do `Style.RESET_ALL otherwise all your code will be that colour
with colorama to apply to text simply do
print(f"{[attribute.style]} text) or + for variables
for more info read this https://pypi.org/project/colorama/
but there's so much you can do with 6 colours...
Using Termcolor
term colour is very similar to colorama in the styling except for the way you execute is slightly different
(i don't use term colour so I am not an expert, please correct me in comments)
import termcolor from termcolour import coloured # any attribute will do thing = colored("text", "magenta") # replace with style
for more info read this https://pypi.org/project/termcolor/
Using Pure ANSI codes
finally no import required , just some base knowledge
Disclaimer i find it hard to read my text when it's with these codes so I put those codes in a separate variable and call them for readability
here's how to call
effectName = "\033[<effect><effect(could be corrosondant like with RGB)>m"
you can mix colours and effects so add as many <effects>
as u need
here is a table of attributes/effects:
ANSI colours
ok styling is great but what we really want are cool colours, and the colours that give us are primitive
so to expand the spectrum we go to 4 bit colours!
(table explains it but I am better)
4-bit colours
from the above, we can make a yellow background with
\033[93;103m
ANother Disclaimer; these ANSI codes work in other languages but slightly different, I will try to make a tutorial in the future.
8-bit colours
Now this is more like it! 256 colours availible
using the able we can make navy like this:
\033[38;4;18m
- the last one is the colour btw
\033[48;5;57m
- another colour
i didn't mention it but you can mix colours too like this:
\033[38;5;206;48;5;57m
RGB colours
unlock the full-colour spectrum! now with RGB
here's how:
\033[38;2;<r>;<g>;<b>m #Select RGB foreground color
we can make a slick orange with:
\033[38;2;255;164;0m
final words
while termcolor and colorama are cool there selection is limited and once you have memorised that that table , you become a god of cool text
thanks for sticking around , if this helped then upvote
- I made this real quick so there might be a few mistakes*
for more info visit: https://en.wikipedia.org/wiki/ANSI_escape_code
anything else just comment below - cya
This is so useful! I never really knew how to use ANSI colors, but this made is so much easier. Thanks!
Ilike your profile pic. Cool code btw.
Very comprehensive!
Cool
Cool!
Wow, Thank you @xolyon
Ur good!
@epicman702 here ya go
@CodingCactus you might wanna read this (I hate termcolour) and yeah
@xolyon i have, thank you
Cool Thanks