Python打印有颜色的字符串
使用Python在命令行或shell终端输出有颜色的字符串,效果如下:
示例代码
CYAN = '\033[36m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
RED = '\033[31m'
NORMAL = '\033[0m' #要以此结尾,还原默认颜色
text = "https://www.perfcode.com"
print(text)
print( CYAN + text + NORMAL) #青色
print( GREEN + text + NORMAL) #绿色
print( YELLOW + text + NORMAL) #黄色
print( BLUE + text + NORMAL) #蓝色
print( RED + text + NORMAL) #红色
print(GREEN + text[:8] + YELLOW + text[8:] + NORMAL) #绿色 + 黄色
需你的终端支持,在某些不支持的IDE下运行程序可能无效。