Terminal Colour in Python
LeonDoesCode (440)


No, not in this one. However, we will be covering GUIs in the next set of tutorials. In this one, we will be covering the Command Line Interface, or CLI for short. We have the ability, with Linux based Systems, to use a thing called ANSI. This allows us to create coloured text in the terminal, which is pretty cool for little programs or terminal based games.


We will be using the colorama module to create the coloured text. This is because it also supports the Windows CLI as well, and the fact that it is super easy to use. So make sure that you install it using pip or Repl's Package Manager. Now we can import it like so:

# import colorama but as cr, as cr is easier to use.
import colorama as cr

One quick thing I'd like to mention about colorama is that you can initialise it. We will be using this method:

# Initialise colorama and make it so that the colours auto reset

So that each print statement has its colours reset. If you want the colours to be kept, then you can remove the autoreset=True or set it to False, which it is by default.

Let's begin!

Let's Do This!

So, we are going to be using the good old Hello, World! to look at how colorama works. So let's begin by making the Hello, look red, and the World! look blue. We will then print Hello, World! after to show that the colours don't continue:

# Print with foreground colors
print(f"{cr.Fore.RED}Hello, {cr.Fore.BLUE}World!")
# Print without colors
print("Hello, World!")

As you can see, we used cr.Fore, this means that we are changing the Foreground colours. You can also change the Background colours of text like so:

# Print with background colors
print(f"{cr.Back.RED}Hello, {cr.Back.BLUE}World!")
# Print without colors
print("Hello, World!")

Pretty cool, but are there any other colours? Yes there are! These are the colours that come with colorama

# All colors




# If you don't use auto reset, you can reset your colors like so

You'll also notice that you can reset your colours. This is for when you don't use autoreset. It allows you to revert the colour back to defaults.

You can also add brightness to your colours. This helps if you need to use a colour more than once, or if you need to be, well, more or less bright:

# Adding opacity to colors
print(f"{cr.Fore.RED}Hello, {cr.Style.DIM}World!")
print(f"{cr.Fore.RED}Hello, {cr.Style.NORMAL}World!")
print(f"{cr.Fore.RED}Hello, {cr.Style.BRIGHT}World!")

We use Style to do this. You'll see a gradient in the brightness from DIM to NORMAL to BRIGHT. We can also reset all our colours and brightness using Style like so:

# Resetting all colors, without autoreset
print(f"{cr.Back.WHITE}{cr.Fore.BLACK}Hello, {cr.Style.RESET_ALL}World!")

Which is very useful to know if you have set all three and don't want to have to reset each one individually.


Being able to change the colour of text in the terminal is very useful. It can be used for a variety of things, making dull text look much more appealing. I hope this helps you in you adventures!

Have a great day!

This tutorial is one in a series of tutorials suggested by @enigma_dev .
If you have any suggestions for tutorials, leave them in the comments and I'll be sure to have a look. If you like one in the comments, then give it an up vote to show that you want to see it. It makes my life so much more easier. Thanks in advance!

You are viewing a single comment. View All
LeonDoesCode (440)

@enigma_dev Thanks! Glad you liked it!