Add Colour to Text in Python

To make some of your text more readable, you can use ANSI escape codes to change the colour of the text output in your python program. A good use case for this is to to highlight errors.

The escape codes are entered right into the print statement.

print("\033[1;32;40m Bright Green  \n")


The above ANSI escape code will set the text colour to bright green. The format is;
\033[  Escape code, this is always the same
1 = Style, 1 for normal.
32 = Text colour, 32 for bright green.
40m = Background colour, 40 is for black.


This table shows some of the available formats;

Text colorCodeText styleCodeBackground colorCode
Black30No effect0Black40



BerryIMU Raspberry Pi Gyroscope Accelerometer



Here is the code used to create the coloured text in the title image;

print("\033[0;37;40m Normal text\n")
print("\033[2;37;40m Underlined text\033[0;37;40m \n")
print("\033[1;37;40m Bright Colour\033[0;37;40m \n")
print("\033[3;37;40m Negative Colour\033[0;37;40m \n")
print("\033[5;37;40m Negative Colour\033[0;37;40m\n")
print("\033[1;37;40m \033[2;37:40m TextColour BlackBackground          TextColour GreyBackground                WhiteText ColouredBackground\033[0;37;40m\n")
print("\033[1;30;40m Dark Gray      \033[0m 1;30;40m            \033[0;30;47m Black      \033[0m 0;30;47m               \033[0;37;41m Black      \033[0m 0;37;41m")
print("\033[1;31;40m Bright Red     \033[0m 1;31;40m            \033[0;31;47m Red        \033[0m 0;31;47m               \033[0;37;42m Black      \033[0m 0;37;42m")
print("\033[1;32;40m Bright Green   \033[0m 1;32;40m            \033[0;32;47m Green      \033[0m 0;32;47m               \033[0;37;43m Black      \033[0m 0;37;43m")
print("\033[1;33;40m Yellow         \033[0m 1;33;40m            \033[0;33;47m Brown      \033[0m 0;33;47m               \033[0;37;44m Black      \033[0m 0;37;44m")
print("\033[1;34;40m Bright Blue    \033[0m 1;34;40m            \033[0;34;47m Blue       \033[0m 0;34;47m               \033[0;37;45m Black      \033[0m 0;37;45m")
print("\033[1;35;40m Bright Magenta \033[0m 1;35;40m            \033[0;35;47m Magenta    \033[0m 0;35;47m               \033[0;37;46m Black      \033[0m 0;37;46m")
print("\033[1;36;40m Bright Cyan    \033[0m 1;36;40m            \033[0;36;47m Cyan       \033[0m 0;36;47m               \033[0;37;47m Black      \033[0m 0;37;47m")
print("\033[1;37;40m White          \033[0m 1;37;40m            \033[0;37;40m Light Grey \033[0m 0;37;40m               \033[0;37;48m Black      \033[0m 0;37;48m")


61 thoughts on “Add Colour to Text in Python”

    1. Thanks!

      Am I the only one that at first thought that that message was from Moriarty? (Some people will get it, some not. Either way, I owe you, you saved my life with this)

    2. This is very helpful so now I can prank my bro by changing his color in his program from white to black ( ͡° ͜ʖ ͡°)

    3. It does not work on mine, I use 3.9.0 and tried both \x1b and \033, only the start got hided and other got displayed, so the color did not display.

      1. try this:

        import os

        if == ‘nt’:
        os.system(“”) # enables ansi characters in Windows terminal

    1. For python three the escape code is “\x1b[” then you add stuff so bright green would be “\x1b[1;32m”
      I hope that helped ya out.

    2. try ‘import sys’ first, then
      print (‘\033[1;33mHello \033[0;0mworld’)
      that worked for me using python3.6 on my android phone with termux as my command line (dash shell I think).
      \033[1;33m –sets color for ‘Hello’
      \033[0;0m — resets color for ‘world’

      1. it doesnt work, only on MAC, please someone help, i needa do this on my Windows pc none of em works

  1. Using python 3.6.3, doesn’t work for me for some reason… checked you’re comments as well. Tried to work it out myself, but couldn’t… I’m just a GCSE student after all 😀 could i get some help please?

  2. The only thing i can not get to worl is the Text Style option. Writing 1,2,3,4 or 5 gives no different results printing the result text. Any comment on this?

    1. This does not work on windows when you call your script from Thonny, Idle, powershell, or cmd. However this does however work if you call it from a terminal that supports it (such as cygwin)

  3. So I am pretty sure this doesn’t work when using raw_input(“”) and adding some colours. Try this out in Python3.x, then type loads of characters until you reach the right side of the screen. It wraps the text before getting there for me. I looked and this is a current bug with python.
    Code with the bug:

    raw_input(” Test : \033[;1m\033[0;0m\033[;1m\033[0;0m\033[;1m\033[0;0m\033[;1m\033[0;0m\033[;1m\033[0;0m\033[;1m\033[0;0m”)

  4. How can I restore the colors from previous terminal? I mean, I was using 40m to add black background, but the terminal is white. I don’t want to use curses, but I need to restore previous terminal behaviour.

  5. suppose i=32
    how do I print the color corresponding to the value stored in the variable i?
    How to write a for a loop to print all the possible colors?

  6. I’m using Python 3.7.2 in Windows 10
    and sadly none of the ideas above work.
    I just want Python code to display text in a specified colour.
    All help appreciated 🙂

    1. Same for me. I am using Spyder (Python 3.7) and none of the ideas are working out. I have tired colorama, termcolor, ANSI escape code method and others too. Can someone please help me out?

    2. You can use ‘ pip install colorama ‘
      then in the script :
      from colorama import Fore
      from colorama import Style
      #to print red text
      print(f'{Fore.RED} hi there {Style.RESET_ALL}’)
      #style reset all

  7. When I run it off of a cmd console (Windows) or PowerShell (Also Windows) it just shows “←[1;30;40m //text//”
    is that not supposed to happen?

    1. Fo me, it shows also a line starting with left arrow and square bracket and no color.

      The colors work in IDE, but not in command prompt where I need it…

      1. the color 3 command makes all the text blue in command prompt!
        color 2 is green
        color 1 in normal

  8. Guess what this is a great little trick BUT if you are using characters from another language with double width characters it will chop the characters in half! How do I get round this? By using “DOCSTRING ” and “UTF” commands?

  9. that was great thank you
    but also I have a question. how can I eliminate the background color?
    bcs u said that by using code : 47m , I can have white, but seems like its grey :\
    what should I do?

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.