Reputation: 101
I am very new to Python/coding in general, and I'm working on a text-based game where two players work together to fight a Boss. It's close to finished, but I'd like to color some of the text.
I looked up my problem, and based on what I found I tried copying and pasting
" print("\033[1;32;40m Bright Green \n")
"
and
" print '\033[1;31mRed like Radish\033[1;m'
"
but neither worked.
I'm coding this in Atom and running it in IDLE.
There were no syntax errors, it simply printed the \033[1;32;40m
along with the text, rather than coloring the text.
Upvotes: 0
Views: 164
Reputation: 10020
You can use good modules like termcolor:
import sys
from termcolor import colored, cprint
text = colored('Hello, World!', 'red')
print(text)
cprint('Hello, World!', 'green', 'on_red')
print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan')
print_red_on_cyan('Hello, World!')
for i in range(10):
cprint(i, 'magenta', end=' ')
cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr)
or 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')
Upvotes: 1