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
相关推荐
初学小刘5 小时前
基于 U-Net 的医学图像分割
python·opencv·计算机视觉
B站计算机毕业设计之家5 小时前
Python招聘数据分析可视化系统 Boss直聘数据 selenium爬虫 Flask框架 数据清洗(附源码)✅
爬虫·python·selenium·机器学习·数据分析·flask
雪碧聊技术5 小时前
爬虫是什么?
大数据·爬虫·python·数据分析
FL16238631295 小时前
[yolov11改进系列]基于yolov11使用fasternet_t0替换backbone用于轻量化网络的python源码+训练源码
python·yolo·php
Freshman小白5 小时前
python算法打包为docker镜像(边缘端api服务)
python·算法·docker
岁岁岁平安5 小时前
python mysql-connector、PyMySQL基础
python·mysql·pymysql
野生技术架构师6 小时前
牛客网Java 高频面试题总结(2025最新版)
java·开发语言·面试
一只鹿鹿鹿6 小时前
系统安全设计方案书(Word)
开发语言·人工智能·web安全·需求分析·软件系统
铁锹少年6 小时前
当多进程遇上异步:一次 Celery 与 Async SQLAlchemy 的边界冲突
分布式·后端·python·架构·fastapi
梨轻巧6 小时前
pyside6常用控件:QCheckBox() 单个复选框、多个复选框、三态模式
python