Purpose: When using Python, change the output color and style in the terminal.
Environment: Ubuntu 16.4 python 3.5.2
Scenario: When writing a small script, if we do not need to output to a file, perhaps just want to display information in the terminal, you can try to change the output text color and style, highlighting or just want to show.
Looked up a little information:
The character color of terminal is controlled by escape sequence, and it is the system display function in text mode, which is independent of the specific language.
The escape sequence starts with the ESC and can do the same with \033 (the ASCII code for ESC is in decimal notation is 27, = 33 in octal).
The format is:
\033[display mode; foreground color; background colour m
Here are 3 parameters:
1)
Display mode: 0 (default), 1 (highlight), 22 (not bold), 4 (underscore), 24 (not underlined),
5 (flashing), 25 (non-flashing), 7 (inverted), 27 (non-inverting)
2)
Foreground: 30 (Black), 31 (red), 32 (green), 33 (yellow), 34 (blue), 35 (Ocean
Red), 36 (cyan), 37 (white)
3)
Background color: 40 (Black), 41 (red), 42 (green), 43 (yellow), 44 (blue), 45 (Ocean
Red), 46 (cyan), 47 (white)
Like what:
\033[0M uses the default style, omitting the front and back colors.
\033[1;32;40m Green
033[1;31;40m Red
Example:
Print (' \033[1;31;40m%s\033[0m '% ' output red character ')
Understand: After changing the settings, restore the settings.
Reference link: MarkLogic Mail reply
This article is from the "Rickyhul" blog, make sure to keep this source http://rickyh.blog.51cto.com/10934856/1953157
Let the print have different colors in Python