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
Red31Bold1Red41
Green32Underline2Green42
Yellow33Negative13Yellow43
Blue34Negative25Blue44
Purple35Purple45
Cyan36Cyan46
White37White47

 

 

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")
\n")

 

62 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 os.name == ‘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?

  10. 3[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”)
    \n”)

    Raspberry Pi controlling a LED cube with Python
    March 8, 2016
    In “Raspberry Pi”
    Using a button and the GPIO on a Raspberry Pi to send a SMS
    September 28, 2019
    In “BerryGPS”
    Raspberry Pi, 1.8″ TFT, RF Receiver and Temperature Sensor
    January 8, 2013
    In “Linux”
    PYTHON
    61 THOUGHTS ON “ADD COLOUR TO TEXT IN PYTHON”
    Joao Matos
    MAY 22, 2015 AT 4:46 PM
    Hello,

    You could also use a module called colorama.
    https://pypi.python.org/pypi/colorama/0.3.3

    Best regards,

    JM

    REPLY
    Ema Ferdová
    AUGUST 27, 2018 AT 2:59 AM
    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)

    REPLY
    PinkiMingo
    MAY 26, 2019 AT 4:52 AM
    You mean from the book, Lock and Key?

    Jessie Smetts
    DECEMBER 6, 2019 AT 9:37 AM
    thx this is much better

    REPLY
    Howard
    AUGUST 25, 2020 AT 4:28 AM
    This is very helpful so now I can prank my bro by changing his color in his program from white to black ( ͡° ͜ʖ ͡°)

    REPLY
    willywillycow
    NOVEMBER 22, 2020 AT 6:36 PM
    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.

    REPLY
    KHAGEN PATEL
    DECEMBER 16, 2020 AT 7:39 AM
    Same here. Any solution ?

    G.R.Curtis
    MAY 15, 2022 AT 7:09 AM
    try this:

    import os

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

    yuvraj
    DECEMBER 26, 2020 AT 5:45 AM
    for me, it does not work

    REPLY
    Henry
    MARCH 30, 2021 AT 10:46 PM
    … It still needs to change the invalid syntax at the end so I need another time to research …

    REPLY
    noobsee
    FEBRUARY 17, 2016 AT 3:17 PM
    it doesn’t work on python 3

    REPLY
    Cluosion
    DECEMBER 4, 2017 AT 3:17 AM
    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.

    REPLY
    python experimenter
    FEBRUARY 21, 2018 AT 5:10 PM
    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’

    REPLY
    Ivor Thomson
    AUGUST 10, 2021 AT 1:03 AM
    What the programme to get colour ansi escape for python

    PoProstuBartek
    DECEMBER 24, 2018 AT 4:19 AM
    it DOES work on python 3

    REPLY
    Muhammad Zubair Ahmad
    NOVEMBER 17, 2021 AT 6:40 AM
    first install colorama in cmd (pip3 install colorama)

    jalipo
    APRIL 26, 2019 AT 6:27 PM
    yes same for me

    REPLY
    Houda
    MAY 2, 2019 AT 12:59 AM
    it did in my side python 3.6.7

    REPLY
    el
    SEPTEMBER 1, 2019 AT 2:20 AM
    me too urgh .. i need to learn it before tomorrow

    REPLY
    CallMeFish
    MAY 6, 2020 AT 3:03 PM
    U r right

    REPLY
    dome
    JUNE 24, 2020 AT 2:16 AM
    it does

    REPLY
    Bionic_Junior
    SEPTEMBER 4, 2020 AT 8:26 PM
    it doesnt work, only on MAC, please someone help, i needa do this on my Windows pc none of em works

    dara
    AUGUST 25, 2016 AT 8:54 PM
    can anything work on python3 for this

    REPLY
    Cluosion
    DECEMBER 4, 2017 AT 3:19 AM
    read my reply to the earlier guy 😛

    REPLY
    python experimenter
    FEBRUARY 21, 2018 AT 5:12 PM
    or mine using sys module

    Breagha Carr
    APRIL 16, 2019 AT 3:31 AM
    yeah this works fine

    REPLY
    Bryan Boot
    FEBRUARY 22, 2017 AT 5:41 PM
    How to save and restore the original terminal colors after changing them?

    REPLY
    Cluosion
    DECEMBER 4, 2017 AT 3:19 AM
    In python 3 its “\x1b[0m” so in raspberry pi it might be “\033[0m” or something like that.

    REPLY
    Francis CamoDude
    NOVEMBER 17, 2017 AT 1:02 AM
    It didn’t work on me
    2.7.14

    REPLY
    HARISH DATLA
    OCTOBER 28, 2018 AT 10:19 PM
    It works on machines dude

    REPLY
    Jake
    NOVEMBER 23, 2017 AT 12:46 AM
    This did not work at all on the most recent version of python

    REPLY
    Cluosion
    DECEMBER 4, 2017 AT 3:18 AM
    read my reply to the earlier guy 😛

    REPLY
    Ratul Hasan
    JUNE 17, 2019 AT 12:45 AM
    Please import sys first. Firstly didn’t work for me either.

    REPLY
    Jake
    DECEMBER 8, 2017 AT 8:18 AM
    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?

    REPLY
    urdad
    NOVEMBER 27, 2018 AT 5:53 PM
    no

    REPLY
    Mr Kemsko
    JANUARY 31, 2018 AT 9:13 PM
    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?

    REPLY
    LulzSec
    MARCH 7, 2018 AT 12:01 AM
    this not working in python3

    REPLY
    Samuel Kim
    MARCH 7, 2018 AT 7:59 AM
    thonny 3.6.1 not working

    REPLY
    parx
    FEBRUARY 1, 2019 AT 11:41 PM
    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)

    REPLY
    the person that corrects all
    MAY 3, 2018 AT 6:58 PM
    Just do import sys before you do it its universal that way 😀

    REPLY
    Kai Farley
    AUGUST 15, 2018 AT 11:47 PM
    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”)

    REPLY
    Eduard Lucena
    OCTOBER 11, 2018 AT 12:00 AM
    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.

    REPLY
    Me
    MAY 25, 2020 AT 12:16 PM
    Apply a white background to it. Done!

    REPLY
    Rajit Datta
    JANUARY 2, 2019 AT 1:22 AM
    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?

    REPLY
    Anuj Agrawal
    APRIL 8, 2019 AT 7:35 PM
    For python 3.x users:
    Just type

    from colorama import init
    init()

    then you are good to go

    REPLY
    Martin.
    MAY 12, 2019 AT 4:50 AM
    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 🙂

    REPLY
    Kshitij
    JUNE 30, 2020 AT 1:10 PM
    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?

    REPLY

    MAY 30, 2021 AT 12:03 AM
    It needz to be in the windows terminal, python shell will not work

    okba elbahi
    FEBRUARY 22, 2022 AT 1:43 AM
    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
    “`

    REPLY
    Tryto
    JUNE 20, 2019 AT 11:28 PM
    Call of python

    REPLY
    Thengakola
    OCTOBER 3, 2020 AT 1:51 PM
    U can use \e instead of \033…
    Like “\e[1;32;40m” instead of “\033[1;32;40m”
    Which is Bright Green btw….

    REPLY
    Pingback: Python 3 bug print background color issue – Ask python questions
    sam
    NOVEMBER 12, 2020 AT 8:08 PM
    how to write to text file?

    REPLY
    Neelam
    NOVEMBER 28, 2020 AT 6:32 PM
    I am not getting response of the following command
    import sys
    print (‘\033[1;33mHello \033[0;0mworld’)

    REPLY
    Joseph
    DECEMBER 12, 2020 AT 1:56 AM
    Hey how would I have an imput as bold because any way i try, it wont work.

    REPLY
    Alyxx
    DECEMBER 15, 2020 AT 3:54 PM
    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?

    REPLY
    Mike
    AUGUST 9, 2021 AT 6:29 PM
    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…

    REPLY
    Aarav
    JULY 27, 2022 AT 5:21 AM
    the color 3 command makes all the text blue in command prompt!
    color 2 is green
    color 1 in normal

    Andrew Dalton
    JULY 14, 2021 AT 6:07 AM
    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?

    REPLY
    Maya
    DECEMBER 8, 2021 AT 5:15 AM
    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?

    REPLY
    Pingback: How To Colour Letters In Python – Electricity Bill Calculator
    LEAVE A REPLY
    Your email address will not be published. Required fields are marked *

    Comment *

    Name *

    Email *

    Website

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

    Blip, blop, bloop…
    BERRYIMU
    BERRYIMU QUICK START GUIDE
    GUIDE TO INTERFACING A GYRO AND ACCELEROMETER WITH A RASPBERRY PI
    CREATE A DIGITAL COMPASS WITH THE RASPBERRY PI – PART 1 – “THE BASICS”
    CREATE A DIGITAL COMPASS WITH THE RASPBERRY PI – PART 2 – “TILT COMPENSATION”
    CREATE A DIGITAL COMPASS WITH THE RASPBERRY PI – PART 3 – “CALIBRATION”
    DIGITAL COMPASS WITH THE RASPBERRY PI – PART 4- “SMARTPHONE REPLICA”
    HOW TO CREATE AN INCLINOMETER USING A RASPBERRY PI AND AN IMU
    GUIDE TO INTERFACING A GYRO AND ACCELEROMETER WITH A RASPBERRY PI – KALMAN FILTER
    CONVERTING VALUES FROM AN ACCELEROMETER TO GS
    DOUBLE TAP DETECTION WITH BERRYIMUV3
    CONNECTING BERRYIMUV3 VIA SPI TO A RASPBERRY PI
    BERRYGPS-IMU
    BERRYGPS-IMU QUICK START GUIDE
    BERRYGPS SETUP GUIDE FOR RASPBERRY PI
    GPS DATA LOGGER USING A BERRYGPS
    USING PYTHON WITH A GPS RECEIVER ON A RASPBERRY PI
    NAVIGATING WITH NAVIT ON THE RASPBERRY PI
    USING U-CENTER TO CONNECT TO THE GPS ON A BERRYGPS-IMU
    ACCESSING GPS VIA I2C
    BERRYGPS-IMU FAQ
    BERRYGPS-GSM
    BERRYGPS-GSM OVERVIEW
    USING GPS AND CONNECTING VIA 3G/2G USING PPP
    CONNECTING VIA 3G/2G USING THE HOLOGRAM CLI
    REAL-TIME GPS TRACKING WITH A RASPBERRY PI
    ASSISTED GPS
    HOW TO ACCESS GPS NMEA SENTENCES THROUGH USB ON THE BERRYGPS-GSM
    USING U-CENTER TO CONNECT TO THE GPS ON BERRYGPS-GSM
    USING A BUTTON AND THE GPIO TO SEND A SMS
    CONTROL THE GPIO USING A SMS FROM A MOBILE PHONE
    PISCREEN
    PISCREEN QUICK START GUIDE
    PISCREEN DRIVER INSTALL INSTRUCTIONS
    ENABLE CONSOLE ON PISCREEN
    ENABLE X WINDOWS ON PISCREEN
    PISCREEN WITH STROKE AND GESTURE RECOGNITION
    VIRTUAL KEYBOARD FOR PISCREEN
    PISCREEN FAQ
    PISCREEN KIT ASSEMBLY INSTRUCTIONS
    RASPBERRY PI ROBOTS
    PIBBOT V2 – BALANCING ROBOT USING A RASPBERRY PI
    SUCCESS WITH A BALANCING ROBOT USING A RASPBERRY PI
    PIBBOT – ROBOTIC PROJECT – PHASE 2 – PROTOTYPE
    TFTS ON THE RASPBERRY PI
    PISCREEN – 3.5″ TFT(480×320) WITH TOUCH CONTROL FOR THE RASPBERRY PI – KICKSTARTER LAUNCHED
    RASPBERRY PI WITH A 3.2″ TFT WITH TOUCH CONTROL (PART 2)
    RASPBERRY PI WITH A 1.8″ TFT SHIELD
    RASPBERRY PI WITH A 2.2″ TFT
    RASPBERRY PI, 1.8″ TFT, RF RECEIVER AND TEMPERATURE SENSOR
    RASPBERRY PI AND TFT DISPLAY
    CONTROLLING THE GPIO ON A RASPBERRY PI WITH A TOUCHSCREEN
    PROGRAMMING FOR A TOUCHSCREEN ON THE RASPBERRY PI

    FOLLOW OZZMAKER VIA EMAIL
    Enter your email address to follow and receive notifications of new posts by email.

    Join 353 other subscribers

    Email Address
    Email Address

    FOLLOW

    FACEBOOK PAGE
    Facebook Page
    Proudly powered by WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *

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