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
相关推荐
nbu04william20 分钟前
Deepseek-api省token的用法
python·大模型·token·deepseek
测试老哥37 分钟前
Pytest自动化测试详解
自动化测试·软件测试·python·测试工具·测试用例·pytest·接口测试
坚持学习前端日记1 小时前
国产化适配全流程适配英伟达本地开发
人工智能·python
jimy11 小时前
C语言模拟对象、方法:“函数指针+结构体“复用函数指针指向的函数体
c语言·开发语言
源图客1 小时前
云途物流API开发-鉴权认证(Java)
java·网络·python
天外天-亮2 小时前
HBuilder X 使用 uview-plus 方式
开发语言·javascript·hbuilder x·uview-plus
木木子222 小时前
[特殊字符] 音乐播放器——状态驱动的多媒体控制
android·开发语言·华为·php·harmonyos
三川6982 小时前
Tkinter库的学习记录06-变量类别
python
wenying_443237442 小时前
Python脚本—1.提取压缩包中的pdf文件
python·ai编程
_Jimmy_2 小时前
SQLAlchemy 复杂 SQL 执行指南与模板库
python·sql