Skip to content
← Back to Community
Styling text in Python
Profile icon
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

image

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:

image

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

image

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

image

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
Profile icon
mararubiabernar
Profile icon
butterles
Profile icon
elipie
Profile icon
MarblesAndMore
Profile icon
skyup
Profile icon
oignons
Profile icon
frissyn
Profile icon
BrooklynnStewar
Profile icon
Navinor
Profile icon
rachelchiuhc
Comments
hotnewtop
Profile icon
YeetMstr

Cool Thanks

Profile icon
NoelB33

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

Profile icon
CodingGoose

Ilike your profile pic. Cool code btw.

Profile icon
Roar123

Very comprehensive!

Profile icon
ARJPEG

Cool

Profile icon
HemzK

Cool!

Profile icon
[deleted]

Wow, Thank you @xolyon
Ur good!

Profile icon
xolyon

@epicman702 here ya go

Profile icon
YeetMstr

@xolyon This is confusing tho

Profile icon
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

Profile icon
YeetMstr

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

Profile icon
xolyon

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

Profile icon
CodingCactus

@xolyon i have, thank you