在python终端中打印颜色 [英] Printing colors in python terminal

查看:29
本文介绍了在python终端中打印颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个在 python 终端中打印颜色的程序,但我不知道如何.我听说您可以使用某些转义序列来打印彩色文本,但我不确定这一点.如何使用 python 终端以特定颜色打印字符串?

I'd like to make a program that prints colors in the python terminal but I don't know how. I've heard that you can use certain escape sequences to print text in color, but I'm not sure of this. How can I print a string in a specific color using the python terminal?

旁注:我运行一个版本的 Linux.

Side note: I run a version of Linux.

推荐答案

试试termcolor 模块.

from termcolor import colored

print colored('hello', 'red'), colored('world', 'green')

请参阅使用 Python 在终端中使用颜色打印?

此外,您可以使用 ANSI 代码:

Also, you could use ANSI codes:

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

    def disable(self):
        self.HEADER = ''
        self.OKBLUE = ''
        self.OKGREEN = ''
        self.WARNING = ''
        self.FAIL = ''
        self.ENDC = ''

print(bcolors.WARNING + "Warning" + bcolors.ENDC)

这篇关于在python终端中打印颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆