Learn to Code via Tutorials on Repl.it!

← Back to all posts
Styling text in Python
xolyon

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

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)

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

Voters
elipie
MarblesAndMore
skyup
oignons
frissyn
BrooklynnStewar
Warhawk947
rachelchiuhc
CodingGoose
AAddams
Comments
hotnewtop
NoelB33

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

CodingGoose

Ilike your profile pic. Cool code btw.

Roar123

Very comprehensive!

ARJPEG

Cool

HemzK

Cool!

[deleted]

Wow, Thank you @xolyon
Ur good!

YeetMstr

Cool Thanks

xolyon

@epicman702 here ya go

YeetMstr

@xolyon This is confusing tho

xolyon

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

YeetMstr

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

xolyon

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

CodingCactus

@xolyon i have, thank you