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
相关推荐
高彬4 分钟前
SAP ECC6.0 对接 C# web Services服务
开发语言·前端·c#·sap
iNeuOS工业互联网9 分钟前
多模态知识库,打通文本、图像与视频资源的协同实践与应用
人工智能·python·音视频
张小姐的猫10 分钟前
【C++开发脚手架】 —— Jsoncpp上手
java·linux·开发语言·网络·c++·tcp/ip·udp
一晌小贪欢10 分钟前
python-第20天:关键字参数与不定长参数
java·开发语言·前端·python·数据可视化·函数参数·python办公
小灰灰搞电子11 分钟前
Rust+Slint 实现蜂巢菜单源码分享
开发语言·rust·蜂巢菜单.
我命由我1234512 分钟前
JS Mock(Mock 拦截请求的前提、Mock 使用、关闭 Mock)
开发语言·前端·javascript·前端框架·html·html5·js
Metaphor69213 分钟前
如何在 Python 环境中裁剪 PDF 页面
python·pdf
半亩码田19 分钟前
C#转Python第4.4篇:JSON 处理:json 模块 vs System.Text.Json
python·c#·json
OPEN-F19 分钟前
C++并发编程:异步任务与线程池实战
开发语言·c++·算法
SelectDB技术团队28 分钟前
统一全文检索与 SQL 分析:Apache Doris 日志分析实践
大数据·数据结构·后端·python·全文检索·doris·日志分析