python输出带颜色的文字

方法一

示例代码:

python 复制代码
# ANSI escape codes for some colors
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
BLUE = '\033[94m'
MAGENTA = '\033[95m'
CYAN = '\033[96m'
WHITE = '\033[97m'
RESET = '\033[0m'  # Resets the color to default

print(f"{RED}This is red text{RESET}")
print(f"{GREEN}This is green text{RESET}")
print(f"{YELLOW}This is yellow text{RESET}")
print(f"{BLUE}This is blue text{RESET}")
print(f"{MAGENTA}This is magenta text{RESET}")
print(f"{CYAN}This is cyan text{RESET}")
print(f"{WHITE}This is white text{RESET}")

效果:

方法二

需要termcolor的支持

bash 复制代码
pip install colorama

示例代码:

python 复制代码
from colorama import Fore, Back, Style, init

# Initialize Colorama
init(autoreset=True)

print(Fore.RED + 'This is red text')
print(Fore.GREEN + 'This is green text')
print(Back.YELLOW + 'This has a yellow background')
print(Style.DIM + 'This is dim text')
print(Style.RESET_ALL)
print('Back to normal text')

效果:

方法三

需要termcolor:

bash 复制代码
pip install termcolor
python 复制代码
from termcolor import colored

print(colored('Hello, World!', 'red'))
print(colored('Green text', 'green'))
print(colored('Blue text', 'blue', 'on_white'))  # Blue text on white background
相关推荐
Honmaple17 小时前
中国四级城市联动数据,包含港澳台,内含json , sql , python 脚本
python·sql·json
BoBoZz1917 小时前
Curvatures 曲率的计算、边缘曲率的调整以及曲率、颜色的映射
python·vtk·图形渲染·图形处理
2501_9307077817 小时前
使用C#代码更改 PowerPoint 幻灯片大小
开发语言·c#·powerpoint
CoderCodingNo17 小时前
【GESP】C++三级真题 luogu-B4414 [GESP202509 三级] 日历制作
开发语言·c++·算法
bug总结17 小时前
前端开发中为什么要使用 URL().origin 提取接口根地址
开发语言·前端·javascript·vue.js·html
少吃零食多运动17 小时前
【Jupyter notebook修改工作目录】
python·jupyter
Swizard18 小时前
别买树莓派了!3步教你在安卓手机上跑通 CPython + PaddleOCR,打造随身 AI 识别终端
python·ai·移动开发
晨曦夜月18 小时前
笔试强训day7
开发语言·c++·算法
Kurbaneli18 小时前
先啃C语言还是直奔目标?
开发语言
weixin_3077791318 小时前
Jenkins Pipeline 完全指南:核心概念、使用详解与最佳实践
开发语言·ci/cd·自动化·jenkins·etl