Python console print color configuration

1. Background:

During the debugging process, the code fragments that need to be focused on need to be displayed emphatically. This article mainly introduces how to set the font color and background color of the console output and print. Note: the
display effect of the console is limited, and it cannot be as cool as the front-end page , can only do some simple settings

2. Principle:

The character color of the Python terminal is controlled by the escape sequence, which is the system display function in the text mode, and has nothing to do with the specific language. It is also applicable in other languages. The escape sequence starts with ESC and can be replaced by \033
( The ASCII code of ESC is 27 in decimal, the corresponding hexadecimal is 0x1B, and the corresponding octal is 033) Note
: This method is only limited to the console-changing the output color, not applicable to the output of the generated EXCEL file

3. Configuration method:

3.1. Grammatical rules:

\033[显示方式;字体颜色;背景色m + "输出主题" + \033[0m

Grammar rules start with \033+\033 at the end, [0m: display for the default terminal

3.2. Values ​​corresponding to font color & background color:

insert image description here

3.3. Display mode:

insert image description here

3.4. Effect display:

print(f"\033[0;31;43m**********redis查询数据成功,数据表名称为:{
      
      name}\033[0m")
print(f"\033[0;33;41m**********redis添加数据成功,数据表名称为:{
      
      key},数据值为:{
      
      value}\33[0m")
print(f"\033[0;31;43m**********redis删除数据成功,数据表名称为:{
      
      keys}\033[0m")
print(f"\033[0;33;41m**********redis添加数据成功,数据表名称为:{
      
      name},数据值为:{
      
      key}:{
      
      value}\33[0m")
print(f"\033[0;31;43m**********redis查询数据成功,数据表名称为:{
      
      name},查询key为:{
      
      key}\33[0m")
print(f"\033[0;33;41m**********删除{
      
      name}{
      
      key}键值对成功\33[0m")

insert image description here

3.5. Add file comments:

# _*_ coding: utf-8 _*_
#!/user/bin/env python
# ********** nice day **********
# time: ${DATE} ${TIME}
# Author: Meng fanrong
# file: ${NAME}.py
# ********** Life is short, You need Python **********

insert image description here

Guess you like

Origin blog.csdn.net/weixin_52358204/article/details/127686971