python

python怎么输出带颜色的文字

小亿
120
2023-08-19 04:53:02
栏目: 编程语言

在Python中,可以使用ANSI转义序列来输出带有颜色的文字。以下是一个示例:

# 常量定义
RESET = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
REVERSE = "\033[7m"
# 前景色
BLACK = "\033[30m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
WHITE = "\033[37m"
# 背景色
BG_BLACK = "\033[40m"
BG_RED = "\033[41m"
BG_GREEN = "\033[42m"
BG_YELLOW = "\033[43m"
BG_BLUE = "\033[44m"
BG_MAGENTA = "\033[45m"
BG_CYAN = "\033[46m"
BG_WHITE = "\033[47m"
# 输出带颜色的文字
print(RED + "Hello, World!" + RESET)

运行以上代码,将输出红色的 “Hello, World!” 文字。

0
看了该问题的人还看了