RandomPhobia RandomPhobia - 1 year ago 112
Bash Question

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.

Answer Source

Please take a look at the answer for the question:

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:

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('back to normal now')

Another option, use termcolor:

from termcolor import colored
print colored('hello', 'red'), colored('world', 'green')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download