user7310293
user7310293

Reputation:

How do I colour text in Python?

If I had this code:

print("Hello World")

How would I change its colour to, say, green? I've tried ANSI escape codes and they don't work. (Python Shell)

Upvotes: 2

Views: 174

Answers (3)

LucG
LucG

Reputation: 1334

You can use the package termcolor

Upvotes: 0

Aaron
Aaron

Reputation: 11075

I presume you're using a windows terminal?

From Wikipedia:

Although hardware text terminals have become increasingly rare in the 21st century, the relevance of the ANSI standard persists because most terminal emulators interpret at least some of the ANSI escape sequences in the output text. One notable exception was the Win32 console component of Microsoft Windows before Windows 10 update TH2.

support for escape codes is dependent on the terminal you're using. try cygwin terminal or something else.

Upvotes: 1

slim
slim

Reputation: 41223

ANSI escape codes work for me:

ANSI_GREEN = '\033[92m'
ANSI_NORMAL = '\033[0m'
print ANSI_GREEN + "Hello world" + ANSI_NORMAL

This does, of course, depend on what kind of terminal is displaying the output.

Upvotes: 2

Related Questions