Learn to Code via Tutorials on Repl.it!

← Back to all posts
How To Style Text With Any Color You Want In Python
JonahElias (157)

How to style text with any color in the RGB spectrum in python

Are you bored of normal, plain white text? Well, in this tutorial,

I am going to show you how to style your text to be any color in the RGB spectrum.

This method won't require any imports.

We will be using ANSI Escape Sequences to make the text any color you want

Get your Codes

The first thing you're going to want to do is to get your RGB codes.

RGB stands for Red, Green, and Blue. These colors can be mixed to create any color. So you will need a red number, a green number, and a blue number.

Any color code picking website that looks like this will work:

Here's the link to one I like:

https://www.rapidtables.com/web/color/RGB_Color.html

Once you have your codes, you will need to create a color variable.

A color variable is a variable that contains all of the information about your color.

You can name it anything, but it is helpful to name it after the color you created.

The template for creating a color is stated below:

color = "\033[38;2;R;G;Bm"

You would substitute each R, G, and B, with the corresponding code for each one.

Each digit is separated by a semi-colon

After the blue code, you would put an "m", no matter what.

Remember the m

Everything before the RGB codes stays the same for every time you are creating a new color.

Example:

If I am trying to create a very basic green, I would first get my codes:

0, 255, 0

Then I would put them inside my color variable:

green = "\033[38;2;0;255;0m"
                   ________ These are my RGB Codes

Now I have green

How to put the color into your text

To put the color into your text, you would concatenate it to the text:

green = "\033[38;2;0;255;0m"
print(green + "This Is Green")

The text would then show up as green, it's that easy!

If you have any questions, comment them

And if you liked the tutorial remember to upvote

Comments
hotnewtop
Barry123 (348)

Wow I didn't know you could use RGB with Python!

JonahElias (157)

@Barry123 I'm glad it helped you learn something new

CodeLongAndPros (1593)

@Barry123 Actually, it’s not python, it’s an ASNI code, which means Windows does not support it, unlike Linux.