美文网首页
python: print out coloured strin

python: print out coloured strin

作者: Andy512 | 来源:发表于2019-06-24 19:15 被阅读0次

https://stackoverflow.com/questions/287871/how-to-print-colored-text-in-terminal-in-python

Define a color class

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

The way to use it:

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

including the start and end parts. The end part is important, otherwise, all the following printing will have the same color as used in current line.

This can work on OS X, linux and windows, already tested on centOS 7, it works well.

相关文章

网友评论

      本文标题:python: print out coloured strin

      本文链接:https://www.haomeiwen.com/subject/msqaqctx.html