Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the color of text in python shell?

I'm aware that this is possible with the os module's os.system("color") function, but this changes the whole terminal what I'm looking for something localized to only a single string or variable output. Something in the standard library would be optimal as I want to use this on multiple computers without have to use py2exe or freeze.

like image 545
RandomPhobia Avatar asked Jun 15 '12 00:06

RandomPhobia


1 Answers

Please take a look at the answer for the question:

  • Print in terminal with colors using Python?

If you can't install addtional modules, you can try to use ANSI-sequences directly. Please note, that the method is not portable, and it's better to use some special module.

print " "+ "\033[01;41m" + " " +"\033[01;46m" + " " + "\033[01;42m"

When you need somthing more powerful, I suggest you using colorama (pypi.python.org/pypi/colorama):

from colorama import Fore, Back, Style
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

Another option, use termcolor:

from termcolor import colored
print colored('hello', 'red'), colored('world', 'green')
like image 89
Igor Chubin Avatar answered Sep 30 '22 06:09

Igor Chubin