python输出带有颜色字体的三种方法

方法一

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# ANSI escape codes for some colors
RED = '33[91m'
GREEN = '33[92m'
YELLOW = '33[93m'
BLUE = '33[94m'
MAGENTA = '33[95m'
CYAN = '33[96m'
WHITE = '33[97m'
RESET = '33[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的支持

1
pip install colorama

示例代码:

1
2
3
4
5
6
7
8
9
10
11
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:

1
pip install termcolor
1
2
3
4
5
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

到此这篇关于python输出带有颜色文字的三种方法的文章就介绍到这了,更多相关python输出带颜色文字内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/code/python/11259.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部