在 Python 终端中打印颜色


在终端中,如果你想让一些文本以彩色模式显示,可以使用 python 编程中的多种方法来实现。

使用 Python 模块

1.termcolor 模块:此模块用于为终端中的输出设置 ANSII 颜色格式。

import sys
from termcolor import colored, cprint
text1 = colored('Hello, Tutorialspoint!', 'blue', attrs=['reverse', 'blink'])
print(text1)
cprint('Hello, Python!', 'blue', 'on_white')
print_red_on_blue = lambda x: cprint(x, 'red', 'on_blue')
print_red_on_blue('Hello, from Data Science!')
print_red_on_blue('Hello, Python!')
for i in range(10):
   cprint(i, 'green', end=' ')
cprint("Attention!", 'blue', attrs=['bold'], file=sys.stderr)

结果

更新时间:2019 年 7 月 30 日

652 次浏览

开启你的 职业生涯

完成课程获取认证

开始
广告
© . All rights reserved.