9

I'm working on a little text-based adventure in Python 3.2 as I learn it in order to practise and become more familiar with the language. Anyway, I want to make it so that when certain actions happen, the color of the print text changes. How do I do this.

For example, the first text I want this to occur for is:

if 'strength' in uniqueskill.lower():
time.sleep(3)
print('As you are a Warrior, I shall supply you with the most basic tools every Warrior needs.')
time.sleep(3)
print('A sword and shield.')
time.sleep(1)
print('You have gained A SWORD AND SHIELD!')
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Jack
  • 381
  • 2
  • 4
  • 5
  • You most likely need to output ANSI escape sequences, as [mentioned in this answer](http://stackoverflow.com/questions/2330245/python-change-text-color-in-shell). – samplebias May 04 '11 at 22:14
  • i suggest not to import external library rather writ your own like this => https://stackoverflow.com/a/70599663/3057246 – Vinod Srivastav Jan 18 '22 at 08:44

2 Answers2

32

Colorama is a great fully cross platform module for printing to the terminal/command line in different colours.

Example:

import colorama
from colorama import Fore, Back, Style

colorama.init()

text = "The quick brown fox jumps over the lazy dog"

print(Fore.RED + text)
print(Back.GREEN + text + Style.RESET_ALL)
print(text)

Gives you:

enter image description here

Acorn
  • 49,061
  • 27
  • 133
  • 172
  • Keep getting an AssertionError. – Jack May 04 '11 at 23:52
  • Edited to use Python3 print statements. Are you still getting the error? If so please paste the code you're running and full stacktrace somewhere. – Acorn May 05 '11 at 00:16
  • Still got an error, code is as follows: import colorama from colorama import Fore, Back, Style colorama.init() notedaction = "You have gained a SWORD AND SHIELD!" if 'strength' in uniqueskill.lower(): time.sleep(3) print('As you are a Warrior, I shall supply you with the most basic tools every Warrior needs.') time.sleep(3) print('A sword and shield.') time.sleep(1) print(Fore.RED + notedaction) – Jack May 05 '11 at 17:53
  • I see the problem was that you were using IDLE: http://stackoverflow.com/questions/5902233/colorama-assertionerror-in-python-3-2/5902356#5902356 – Acorn May 05 '11 at 22:39
  • great works in windows, thank you – Jackssn Dec 20 '16 at 10:27
  • Still working perfectly. Thanks. Ps: do not be a doofus like I and forget to `colorama.init()`! – pookie Jun 06 '17 at 13:31
6

You didn't specify your platform, which is quite important here, since most methods for outputting color text to console are platform specific. The curses library which comes with Python, for instance, is UNIX-only, and ANSI codes, don't work on new versions of Windows anymore. The most cross platform solution I can think of, is to install the Windows version of curses on Windows machines and use it.

Here's an example for using color with curses:

import curses

# initialize curses
stdscr = curses.initscr()
curses.start_color()

# initialize color #1 to Blue with Cyan background
curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_CYAN)

stdscr.addstr('A sword and a shield.', curses.color_pair(1))
stdscr.refresh()

# finalize curses
curses.endwin()

Please note that curses is more complex than just having colors. You can use it to define several windows on the console screen, position text using absolute or relative coordinates, manipulate keyboard input and so on. You can find a tutorial here: http://docs.python.org/dev/howto/curses.html

Boaz Yaniv
  • 6,334
  • 21
  • 30
  • I'm on Windows 7 and eventually figured it out. I installed the Colorama library and used the code: print('\x1b[34m' + 'YOU HAVE GAINED A SWORD AND SHIELD!'). The problem is that the final product comes out as '34mYOU HAVE GAINED A SWORD AND SHIELD!' but it is the right color. Any way to hide the letter and numbers? – Jack May 04 '11 at 22:38
  • @Crashdown - example got cut while posting, read edit. – Boaz Yaniv May 04 '11 at 22:40
  • I would prefer using curses to plain ANSI codes for two reasons: they're ugly (especially when you mix with your test), and curses is more powerful. Anyway, I don't know how to make them work properly in python on Windows and I wouldn't bother. If I needed a Windows-only implementation I'd use something like [WConio](http://newcenturycomputers.net/projects/wconio.html) or [console](http://effbot.org/downloads/) – Boaz Yaniv May 04 '11 at 22:45
  • You'd have to compile them yourself though, since it doesn't seems like there's a python 3.2 version for them. – Boaz Yaniv May 04 '11 at 22:46
  • @Boaz: See my answer for how to use colorama. – Acorn May 04 '11 at 23:22