共計 542 個字符,預計需要花費 2 分鐘才能閱讀完成。
可以使用 ANSI 轉義碼來在終端中著色輸出文字。以下是使用 ANSI 轉義碼在 Python 中實現文字著色的示例代碼:
# ANSI 轉義碼顏色代碼
class colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
# 輸出帶顏色的文字
print(colors.OKGREEN + "This text is green." + colors.ENDC)
print(colors.WARNING + "This text is yellow." + colors.ENDC)
print(colors.FAIL + "This text is red." + colors.ENDC)
在上面的示例中,colors類定義了不同顏色的 ANSI 轉義碼。你可以使用 colors.OKGREEN、colors.WARNING、colors.FAIL 等顏色代碼來給輸出的文字著色。記得在需要恢復默認顏色之前使用 colors.ENDC 來結束顏色設置。
丸趣 TV 網 – 提供最優質的資源集合!
正文完