python print Highlight - log output color control

First, grammar

1. The implementation process

It is the terminal character color escape sequence control system is independent of the function mode display text, and specific language . Color control character escape sequence begins with ESC, that is used \033to complete

2. Writing Process

Beginning: \033[显示方式;前景色;背景色m
the end of the section:\033[0m

  • Note: The
    three parameters of the beginning:
  • (1) Display mode, foreground color, background color is an optional parameter, can be written only one of them;
  • (2) In addition, as represented three different meanings of the parameter values ​​is not only repeated, so that the sequence of writing the three parameters required is not fixed, the system can identify; however, it is recommended according to the default format specification writing.
  • (3) the end part of it can be omitted, but for writing specifications, recommended \033[*** beginning, \033[0mthe end.

3. Parameters

Display: 0(默认值)、1(高亮)、22(非粗体)、4(下划线)、24(非下划线)、 5(闪烁)、25(非闪烁)、7(反显)、27(非反显)
Foreground color: 30(黑色)、31(红色)、32(绿色)、 33(黄色)、34(蓝色)、35(紫色)、36(青色)、37(白色)
Background color:40(黑色)、41(红色)、42(绿色)、 43(黄色)、44(蓝色)、45(紫色)、46(青色)、47(白色)

Word background color range: 40--49 Word Color: 30--39
40 (black) 30 (black)
41 (red) 31 (red)
42 (green) 32 (green)
43 (yellow) 33 (Yellow)
44 (blue) 34 (blue)
45 (purple) 35 (purple)
46 (blue) 36 (blue)
47 (White) 37 (White)

4. common format beginning

\033[0mThe default font is displayed properly, do not highlight
\033[32;0min red font display properly
\033[1;32;40mdisplay: Highlight Font color: green background color: Black
\033[0;31;46mDisplay: Normal font color: red background color: cyan

II. Display Example

# 高亮显示输出

s = "hello, world"

# 默认字体输出:
print("默认输出:",'\033[0m%s\033[0m' % s)
print("添加背景:",'\033[0;47m%s\033[0m' % s)

# 高亮显示:
print('\033[1;31;40m%s\033[0m' % s)
print('\033[1;32;40m%s\033[0m' % s)
print('\033[1;33;40m%s\033[0m' % s)
print('\033[1;34;40m%s\033[0m' % s)
print('\033[1;35;40m%s\033[0m' % s)
print('\033[1;36;40m%s\033[0m' % s)
print('\033[1;37;40m%s\033[0m' % s)

Here Insert Picture Description

Published 146 original articles · won praise 66 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_38923792/article/details/102833601