共計 456 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Python 中,可以使用第三方庫 colorama
來實現在命令行中輸出有顏色的字體。可以使用 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 系統中,需要額外調用 init()
方法來初始化 colorama
庫。
丸趣 TV 網 – 提供最優質的資源集合!
正文完