Learn to Code via Tutorials on Repl.it!

← Back to all posts
Styling text in Python
xolyon (346)

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
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:


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:

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

NoelB33 (351)

This is so useful! I never really knew how to use ANSI colors, but this made is so much easier. Thanks!

DannyIsCoding (698)

Ilike your profile pic. Cool code btw.

Roar123 (443)

Very comprehensive!


Wow, Thank you @xolyon
Ur good!

epicman702 (506)

@xolyon This is confusing tho

xolyon (346)

@epicman702 it's not, read it if you want a really in depth tutorial i can invite you to a project an show u

epicman702 (506)

@xolyon Yeah can u invite me to a project? It makes a bit of sense tho

xolyon (346)

@CodingCactus you might wanna read this (I hate termcolour) and yeah