RandomPhobia RandomPhobia - 4 months ago 9
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

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(Style.RESET_ALL)
print('back to normal now')

Another option, use termcolor:

from termcolor import colored
print colored('hello', 'red'), colored('world', 'green')
Comments