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.
Asked
Active
Viewed 4.5k times
2 Answers
6
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
(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')

Community
- 1
- 1

Igor Chubin
- 61,765
- 13
- 122
- 144
-
The above code prints ←[01;41m ←[01;46m ←[01;42m, I've heard ascii escapes aren't supported on all systems, but I've tried it on Windows 7 and XP and neither worked. Any suggestions? – RandomPhobia Jun 19 '12 at 12:14