我之所以做出回应,是因为我找到了一种在Windows上使用ANSI代码的方法,这样你就可以更改文本的颜色而无需任何内置模块:
进行此操作的行是
os.system('color'),但是要确保如果此人不在Windows上,则不会引起错误,你可以使用以下脚本:import os, sysif sys.platform.lower() == "win32": os.system('color')# Group of Different functions for different stylesclass style(): BLACK = lambda x: ' 33[30m' + str(x) RED = lambda x: ' 33[31m' + str(x) GREEN = lambda x: ' 33[32m' + str(x) YELLOW = lambda x: ' 33[33m' + str(x) BLUE = lambda x: ' 33[34m' + str(x) MAGENTA = lambda x: ' 33[35m' + str(x) CYAN = lambda x: ' 33[36m' + str(x) WHITE = lambda x: ' 33[37m' + str(x) UNDERLINE = lambda x: ' 33[4m' + str(x) RESET = lambda x: ' 33[0m' + str(x)print(style.YELLOW("Hello, ") + style.RESET("World!"))注意:尽管此选项与其他Windows选项具有相同的选项,但是Windows即使使用此技巧也无法完全支持ANSI代码。并非所有的文本装饰颜色都起作用,并且所有“明亮”颜色(代码90-97和100-107)显示的颜色与常规颜色相同(代码30-37和40-47)
tl; dr:
os.system('color')在imports 之后添加。Python版本: 3.6.7



