Python format output (color), python format

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.