方法一:
顏色的控制通過ESC字符(\033)加”[“加顏色代碼加”m”實現。ESC的ASCII碼是十進制的27,八進制的033(\033)。
輸出方式由三部分組成
\033[字體顯示方式;字體顏色;字體背景色m'字符'\033[0m
- 顯示方式: 0(默認值)、1(高亮)、22(非粗體)、4(下划線)、24(非下划線)、 5(閃爍)、25(非閃爍)、7(反顯)、27(非反顯)
- 字體色: 30(黑色)、31(紅色)、32(綠色)、 33(黃色)、34(藍色)、35(洋 紅)、36(青色)、37(白色)
- 字體背景色: 40(黑色)、41(紅色)、42(綠色)、 43(黃色)、44(藍色)、45(洋 紅)、46(青色)、47(白色)
name = '野生程序猿'
print(f"\033[0;31m{name}\033[0m") #輸出紅色的字體
print(f"\033[0;31;42m{name}\033[0m") #輸出紅色的字體,背景色為綠色

方式二:
使用colorama模塊,安裝pip install colorama
colorama是一個python專門用來在控制台、命令行輸出彩色文字的模塊,可以跨平台使用。
可用格式常數:可用的格式常數是:
Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Style: DIM, NORMAL, BRIGHT, RESET_ALL
from colorama import Fore,Back,Style
print (Fore.RED + "some red text")
print (Back.GREEN + "and with a green background")
print (Style.DIM + "and in dim text")
print (Style.RESET_ALL)
print ("back to normal now!!")

參考或轉載:
https://blog.csdn.net/qq_44159028/article/details/115400065
https://blog.csdn.net/sinat_38682860/article/details/89977064?dist_request_id=1332041.22626.16193151946081447&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromMachineLearnPai2~default-2.control
https://blog.csdn.net/u014470361/article/details/81512330
https://www.cnblogs.com/xieshengsen/p/6932233.html
