How does python output colored fonts in the cmd command line window

  • method one

The output mode consists of three parts

\033[ Font display mode ; font color ; font background color m'character'\033[0m

  • Display mode: 0 (default value), 1 (highlight), 22 (non-bold), 4 (underline), 24 (non-underline), 5 (flashing), 25 (non-flashing), 7 (reverse), 27 (non-inverted)
  • Font color: 30 (black), 31 (red), 32 (green), 33 (yellow), 34 (blue), 35 (magenta), 36 (cyan), 37 (white)
  • Font background color: 40 (black), 41 (red), 42 (green), 43 (yellow), 44 (blue), 45 (magenta), 46 (cyan), 47 (white)
from colorama import init
init(autoreset=True)
name = '谢辰辰'
print(f"\033[0;31m{name}\033[0m")      #输出红色的字体
print(f"\033[0;31;42m{name}\033[0m")   #输出红色的字体,背景色为绿色

  • Method Two

Use Fore to set the color

from colorama import init,Fore
init(autoreset=True)
print (Fore.BLUE+'遇见你') 
print(Fore.RED+'谢辰辰')

 

 

Guess you like

Origin blog.csdn.net/qq_44159028/article/details/115400065