Python format output (color), python format
Introduction:
If you want to display the output color in Python, it is quite easy to implement. You need termcolor knowledge!
Reference: https://pypi.python.org/pypi/termcolor/1.1.0
Enable:
Shell> pip install termcolor # If this module is not available, first install shell> ipython # Enter ipythonIn [1]: import termcolor # import this module In [2]: termcolor. termcolor. ATTRIBUTES termcolor. HIGHLIGHTS termcolor. VERSION termcolor. cprint termcolor. print_functiontermcolor.COLORS termcolor. RESET termcolor. colored termcolor. OS
# The above is the method of this module. The most common method should be. colored.
Example:
In [1]: from termcolor import colored # Only import this method, because others do not use In [2]: colored ?? # Check which parameters are supported. Of course, there are examples provided by the program below. Signature: colored (text, color = None, on_color = None, attrs = None) is not pasted here) in [3]: text = colored ('Hello world', 'red') # The first parameter is the text to be output, the second parameter is to set the color of the text In [4]: print (text) # output the red Hello WorldHello WorldIn [5]: print (colored ('Hello world ', 'green') # To make it simpler, output the green Hello WorldHello WorldIn [6]: code_yellow = lambda x: colored (x, 'yellow ') # You can use the anonymous function to abstract the color so that you can call In [7]: print (code_ye Llow ('Hello World') # output yellow Hello WorldHello WorldIn [8]: print (colored ('Hello world', 'red', 'on _ yellow ')) # output the red Hello World. The background color is yellow Hello WorldIn [9]: print (colored ('hello, world', 'yellow', attrs = ['reverse ', 'blink']) # The official statement is that the Code is flashing. I don't have a flash. It works the same way as setting the background color! Hello, WorldIn [10]: print (colored ('% s' % 'Hello world', 'red', 'on _ yellow') # In addition, it can also be output formatted, that is, % s % text, which is very convenient! Hello World