python - How to print colored text to the terminal

ID : 258

viewed : 270

Tags : pythonterminaloutputansi-colorspython





Top 5 Answer for python - How to print colored text to the terminal

vote vote

91

This somewhat depends on what platform you are on. The most common way to do this is by printing ANSI escape sequences. For a simple example, here's some Python code from the Blender build scripts:

class bcolors:     HEADER = '\033[95m'     OKBLUE = '\033[94m'     OKCYAN = '\033[96m'     OKGREEN = '\033[92m'     WARNING = '\033[93m'     FAIL = '\033[91m'     ENDC = '\033[0m'     BOLD = '\033[1m'     UNDERLINE = '\033[4m' 

To use code like this, you can do something like:

print(bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC) 

Or, with Python 3.6+:

print(f"{bcolors.WARNING}Warning: No active frommets remain. Continue?{bcolors.ENDC}") 

This will work on unixes including OS X, Linux and Windows (provided you use ANSICON, or in Windows 10 provided you enable VT100 emulation). There are ANSI codes for setting the color, moving the cursor, and more.

If you are going to get complicated with this (and it sounds like you are if you are writing a game), you should look into the "curses" module, which handles a lot of the complicated parts of this for you. The Python Curses HowTO is a good introduction.

If you are not using extended ASCII (i.e., not on a PC), you are stuck with the ASCII characters below 127, and '#' or '@' is probably your best bet for a block. If you can ensure your terminal is using a IBM extended ASCII character set, you have many more options. Characters 176, 177, 178 and 219 are the "block characters".

Some modern text-based programs, such as "Dwarf Fortress", emulate text mode in a graphical mode, and use images of the classic PC font. You can find some of these bitmaps that you can use on the Dwarf Fortress Wiki see (user-made tilesets).

The Text Mode Demo Contest has more resources for doing graphics in text mode.

vote vote

90

There is also the Python termcolor module. Usage is pretty simple:

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

Or in Python 3:

print(colored('hello', 'red'), colored('world', 'green')) 

It may not be sophisticated enough, however, for game programming and the "colored blocks" that you want to do...

vote vote

78

The answer is Colorama for all cross-platform coloring in Python.

It supports Python 3.5+ as well as Python 2.7.

And as of January 2021 it is maintained.

Example screenshot: example screenshot

vote vote

60

Print a string that starts a color/style, then the string, and then end the color/style change with '\x1b[0m':

print('\x1b[6;30;42m' + 'Success!' + '\x1b[0m') 

Success with green background example

Get a table of format options for shell text with the following code:

def print_format_table():     """     prints table of formatted text format options     """     for style in range(8):         for fg in range(30,38):             s1 = ''             for bg in range(40,48):                 format = ';'.join([str(style), str(fg), str(bg)])                 s1 += '\x1b[%sm %s \x1b[0m' % (format, format)             print(s1)         print('\n')  print_format_table() 

Light-on-dark example (complete)

Enter image description here

Dark-on-light example (partial)

Top part of output

vote vote

57

Define a string that starts a color and a string that ends the color. Then print your text with the start string at the front and the end string at the end.

CRED = '\033[91m' CEND = '\033[0m' print(CRED + "Error, does not compute!" + CEND) 

This produces the following in Bash, in urxvt with a Zenburn-style color scheme:

Output colors

Through experimentation, we can get more colors:

Color matrix

Note: \33[5m and \33[6m are blinking.

This way we can create a full color collection:

CEND      = '\33[0m' CBOLD     = '\33[1m' CITALIC   = '\33[3m' CURL      = '\33[4m' CBLINK    = '\33[5m' CBLINK2   = '\33[6m' CSELECTED = '\33[7m'  CBLACK  = '\33[30m' CRED    = '\33[31m' CGREEN  = '\33[32m' CYELLOW = '\33[33m' CBLUE   = '\33[34m' CVIOLET = '\33[35m' CBEIGE  = '\33[36m' CWHITE  = '\33[37m'  CBLACKBG  = '\33[40m' CREDBG    = '\33[41m' CGREENBG  = '\33[42m' CYELLOWBG = '\33[43m' CBLUEBG   = '\33[44m' CVIOLETBG = '\33[45m' CBEIGEBG  = '\33[46m' CWHITEBG  = '\33[47m'  CGREY    = '\33[90m' CRED2    = '\33[91m' CGREEN2  = '\33[92m' CYELLOW2 = '\33[93m' CBLUE2   = '\33[94m' CVIOLET2 = '\33[95m' CBEIGE2  = '\33[96m' CWHITE2  = '\33[97m'  CGREYBG    = '\33[100m' CREDBG2    = '\33[101m' CGREENBG2  = '\33[102m' CYELLOWBG2 = '\33[103m' CBLUEBG2   = '\33[104m' CVIOLETBG2 = '\33[105m' CBEIGEBG2  = '\33[106m' CWHITEBG2  = '\33[107m' 

Here is the code to generate the test:

x = 0 for i in range(24):   colors = ""   for j in range(5):     code = str(x+j)     colors = colors + "\33[" + code + "m\\33[" + code + "m\033[0m "   print(colors)   x = x + 5 

Top 3 video Explaining python - How to print colored text to the terminal







Related QUESTION?