溫馨提示×

python無法輸出有顏色的字體怎么解決

小億
164
2023-10-25 09:31:04
欄目: 編程語言

在Python中,可以使用第三方庫colorama來實現(xiàn)在命令行中輸出有顏色的字體。可以使用pip命令來安裝colorama庫:

pip install colorama

安裝完成后,可以使用以下示例代碼來輸出有顏色的字體:

from colorama import init, Fore, Back, Style

# 初始化colorama
init()

# 輸出紅色的文字
print(Fore.RED + 'This text is red')

# 輸出綠色的背景色和黃色的文字
print(Back.GREEN + Fore.YELLOW + 'This text has a green background and yellow text')

# 重置樣式
print(Style.RESET_ALL + 'This text has the default style')

這樣就可以在命令行中輸出有顏色的字體了。注意,在Windows系統(tǒng)中,需要額外調(diào)用init()方法來初始化colorama庫。

0