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 color | Code | Text style | Code | Background color | Code |
---|---|---|---|---|---|
Black | 30 | No effect | 0 | Black | 40 |
Red | 31 | Bold | 1 | Red | 41 |
Green | 32 | Underline | 2 | Green | 42 |
Yellow | 33 | Negative1 | 3 | Yellow | 43 |
Blue | 34 | Negative2 | 5 | Blue | 44 |
Purple | 35 | Purple | 45 | ||
Cyan | 36 | Cyan | 46 | ||
White | 37 | White | 47 |
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")
Hello,
You could also use a module called colorama.
https://pypi.python.org/pypi/colorama/0.3.3
Best regards,
JM
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)
You mean from the book, Lock and Key?
thx this is much better
This is very helpful so now I can prank my bro by changing his color in his program from white to black ( ͡° ͜ʖ ͡°)
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.
Same here. Any solution ?
for me, it does not work
it doesn't work on python 3
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.
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'
it DOES work on python 3
yes same for me
it did in my side python 3.6.7
me too urgh .. i need to learn it before tomorrow
U r right
it does
it doesnt work, only on MAC, please someone help, i needa do this on my Windows pc none of em works
can anything work on python3 for this
read my reply to the earlier guy 😛
or mine using sys module
yeah this works fine
How to save and restore the original terminal colors after changing them?
In python 3 its "\x1b[0m" so in raspberry pi it might be "\033[0m" or something like that.
It didn't work on me
2.7.14
It works on machines dude
This did not work at all on the most recent version of python
read my reply to the earlier guy 😛
Please import sys first. Firstly didn't work for me either.
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?
no
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?
this not working in python3
thonny 3.6.1 not working
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)
Just do import sys before you do it its universal that way 😀
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")
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.
Apply a white background to it. Done!
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?
For python 3.x users:
Just type
from colorama import init
init()
then you are good to go
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 🙂
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?
Call of python
U can use \e instead of \033...
Like "\e[1;32;40m" instead of "\033[1;32;40m"
Which is Bright Green btw....
how to write to text file?
I am not getting response of the following command
import sys
print ('\033[1;33mHello \033[0;0mworld')
Hey how would I have an imput as bold because any way i try, it wont work.
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?