python输出带有颜色字体的三种方法-kb88凯时官网登录

来自:网络
时间:2024-06-07
阅读:

方法一

示例代码:

# 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}")

效果:

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

方法二

需要termcolor的支持

pip install colorama

示例代码:

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')

效果:

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

方法三

需要termcolor:

pip install termcolor
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
返回顶部
顶部
网站地图