How to print colored text in Python?

前端 未结 30 2828
谎友^
谎友^ 2020-11-21 04:41

How can I output colored text to the terminal in Python?

相关标签:
30条回答
  • 2020-11-21 05:27

    Here's a curses example:

    import curses
    
    def main(stdscr):
        stdscr.clear()
        if curses.has_colors():
            for i in xrange(1, curses.COLORS):
                curses.init_pair(i, i, curses.COLOR_BLACK)
                stdscr.addstr("COLOR %d! " % i, curses.color_pair(i))
                stdscr.addstr("BOLD! ", curses.color_pair(i) | curses.A_BOLD)
                stdscr.addstr("STANDOUT! ", curses.color_pair(i) | curses.A_STANDOUT)
                stdscr.addstr("UNDERLINE! ", curses.color_pair(i) | curses.A_UNDERLINE)
                stdscr.addstr("BLINK! ", curses.color_pair(i) | curses.A_BLINK)
                stdscr.addstr("DIM! ", curses.color_pair(i) | curses.A_DIM)
                stdscr.addstr("REVERSE! ", curses.color_pair(i) | curses.A_REVERSE)
        stdscr.refresh()
        stdscr.getch()
    
    if __name__ == '__main__':
        print "init..."
        curses.wrapper(main)
    
    0 讨论(0)
  • 2020-11-21 05:28

    I have wrapped @joeld answer into a module with global functions that I can use anywhere in my code.

    file: log.py

    def enable():
        HEADER = '\033[95m'
        OKBLUE = '\033[94m'
        OKGREEN = '\033[92m'
        WARNING = '\033[93m'
        FAIL = '\033[91m'
        ENDC = '\033[0m'
        BOLD = "\033[1m"
        
    def disable():
        HEADER = ''
        OKBLUE = ''
        OKGREEN = ''
        WARNING = ''
        FAIL = ''
        ENDC = ''
        
    def infog(msg):
        print(OKGREEN + msg + ENDC)
    
    def info(msg):
        print(OKBLUE + msg + ENDC)
    
    def warn(msg):
        print(WARNING + msg + ENDC)
    
    def err(msg):
        print(FAIL + msg + ENDC)
    
    enable()
    

    Use as follows:

    import log
    log.info("Hello World")
    log.err("System Error")
    
    0 讨论(0)
  • 2020-11-21 05:30

    For Windows you cannot print to console with colors unless you're using the win32api.

    For Linux it's as simple as using print, with the escape sequences outlined here:

    Colors

    For the character to print like a box, it really depends on what font you are using for the console window. The pound symbol works well, but it depends on the font:

    #
    
    0 讨论(0)
  • 2020-11-21 05:30

    If you are programming a game perhaps you would like to change the background color and use only spaces? For example:

    print " "+ "\033[01;41m" + " " +"\033[01;46m"  + "  " + "\033[01;42m"
    
    0 讨论(0)
  • 2020-11-21 05:31

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

    A Python 3.6 example screenshot:

    0 讨论(0)
  • 2020-11-21 05:31

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

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

    Get a table of format options for shell text with 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)

    Dark-on-light example (partial)

    top part of output

    0 讨论(0)
提交回复
热议问题