在Python中,更改字体颜色可以通过以下几种方法实现:
使用ANSI转义序列
print("\033[31m红色字体\033[0m") 红色print("\033[32m绿色字体\033[0m") 绿色print("\033[33m黄色字体\033[0m") 黄色print("\033[34m蓝色字体\033[0m") 蓝色print("\033[35m紫色字体\033[0m") 紫色print("\033[36m青色字体\033[0m") 青色print("\033[37m灰色字体\033[0m") 灰色print("\033[38m默认字体\033[0m") 默认
使用第三方库colorama
from colorama import Foreprint(Fore.RED + "红色字体" + Fore.RESET)print(Fore.GREEN + "绿色字体" + Fore.RESET)print(Fore.BLUE + "蓝色字体" + Fore.RESET)

使用函数定义
def colored_text(text, color):colors = {'reset': '\033[0m','red': '\033[91m','green': '\033[92m','yellow': '\033[93m','blue': '\033[94m',可以根据需要添加更多颜色}return f"{colors[color]}{text}{colors['reset']}"print(colored_text("红色文字", "red"))
以上方法都可以在控制台终端中改变输出的字体颜色。您可以根据需要选择合适的方法
