Python with color output text

Source: Internet
Author: User

We can make eye-catching effect by setting different colors for useful information, because I usually develop under Linux, and the colors in Linux terminal are controlled by escape sequence, the escape sequence starts with ESC and can do the same work with \033 ( The ASCII code of ESC is expressed in decimal notation is 27, which equals 33 in octal notation.

The writing format, and the related instructions are as follows:

Copy Code The code is as follows:


Format: \033[display mode; foreground color; background colour m

Description
Foreground background color
---------------------------------------
30 40 Black
31 41 Red
32 42 Green
33 43 Yellow
34 44 Blue
35 45 Purplish red
36 46 Cyan Blue
37 47 White
Meaning of display mode
-------------------------
0 Terminal default settings
1 highlighting
4 using underscores
5 Flashing
7 Anti-white display
8 Not visible

Example:
\033[1;31;40m <!--1-highlight 31-foreground color Red 40-background black--
\033[0m <!--Use the terminal default setting, which cancels the color setting-- 

The Python code is pasted as follows:

#!/usr/bin/env pythonfrom Collections Import ordereddict#red color-->print ' \033[1;31;40m ' print ' \033[1;32;47m ' print ' output dict have ordered ' print ' \033[0m ' d=ordereddict () d[' foo ']=1d[' bar ']=2d[' spam ']=3d[' grok ']=4for key in D : Print Key,d[key]print ' \033[1;32;47m ' print '-------' print ' delete Elemente from list ' print ' \033[0m ' def dedupe (items) : Seen=set () for item in Items:if item not in Seen:yield item Seen.add (item) a=[1,5,2, 1,9,1,5,10]print List (Dedupe (a))

Python with color output text

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.