今天就跟大家聊聊有关使用Python怎么在终端输出彩色字符,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
#coding:gbk
# ------------------------------------------------
# python终端显示彩色字符类,可以调用不同的方法
# 选择不同的颜色.使用方法看示例代码就很容易明白.
# ------------------------------------------------
#
# 显示格式: \033[显示方式;前景色;背景色m
# ------------------------------------------------
# 显示方式 说明
# 0 终端默认设置
# 1 高亮显示
# 4 使用下划线
# 5 闪烁
# 7 反白显示
# 8 不可见
# 22 非粗体
# 24 非下划线
# 25 非闪烁
#
# 前景色 背景色 颜色
# 30 40 黑色
# 31 41 红色
# 32 42 绿色
# 33 43 黃色
# 34 44 蓝色
# 35 45 紫红色
# 36 46 青蓝色
# 37 47 白色
# ------------------------------------------------
class Colored(object):
# 显示格式: \033[显示方式;前景色;背景色m
# 只写一个字段表示前景色,背景色默认
RED = '\033[31m' # 红色
GREEN = '\033[32m' # 绿色
YELLOW = '\033[33m' # 黄色
BLUE = '\033[34m' # 蓝色
FUCHSIA = '\033[35m' # 紫红色
CYAN = '\033[36m' # 青蓝色
WHITE = '\033[37m' # 白色
#: no color
RESET = '\033[0m' # 终端默认颜色
def color_str(self, color, s):
return '{}{}{}'.format(
getattr(self, color),
s,
self.RESET
)
def red(self, s):
return self.color_str('RED', s)
def green(self, s):
return self.color_str('GREEN', s)
def yellow(self, s):
return self.color_str('YELLOW', s)
def blue(self, s):
return self.color_str('BLUE', s)
def fuchsia(self, s):
return self.color_str('FUCHSIA', s)
def cyan(self, s):
return self.color_str('CYAN', s)
def white(self, s):
return self.color_str('WHITE', s)
# ----------使用示例如下:-------------
color = Colored()
print color.red('I am red!')
print color.green('I am gree!')
print color.yellow('I am yellow!')
print color.blue('I am blue!')
print color.fuchsia('I am fuchsia!')
print color.cyan('I am cyan!')
print color.white('I am white')
颜色对比图(根据需要自己设置对应的值):
运行效果:
# -----------------colorama模块的一些常量---------------------------
# Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
# Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
# Style: DIM, NORMAL, BRIGHT, RESET_ALL
#
from colorama import init, Fore, Back, Style
init(autoreset=True)
class Colored(object):
# 前景色:红色 背景色:默认
def red(self, s):
return Fore.RED + s + Fore.RESET
# 前景色:绿色 背景色:默认
def green(self, s):
return Fore.GREEN + s + Fore.RESET
# 前景色:黄色 背景色:默认
def yellow(self, s):
return Fore.YELLOW + s + Fore.RESET
# 前景色:蓝色 背景色:默认
def blue(self, s):
return Fore.BLUE + s + Fore.RESET
# 前景色:洋红色 背景色:默认
def magenta(self, s):
return Fore.MAGENTA + s + Fore.RESET
# 前景色:青色 背景色:默认
def cyan(self, s):
return Fore.CYAN + s + Fore.RESET
# 前景色:白色 背景色:默认
def white(self, s):
return Fore.WHITE + s + Fore.RESET
# 前景色:黑色 背景色:默认
def black(self, s):
return Fore.BLACK
# 前景色:白色 背景色:绿色
def white_green(self, s):
return Fore.WHITE + Back.GREEN + s + Fore.RESET + Back.RESET
color = Colored()
print color.red('I am red!')
print color.green('I am gree!')
print color.yellow('I am yellow!')
print color.blue('I am blue!')
print color.magenta('I am magenta!')
print color.cyan('I am cyan!')
print color.white('I am white!')
print color.white_green('I am white green!')
运行效果:
Python是一种编程语言,内置了许多有效的工具,Python几乎无所不能,该语言通俗易懂、容易入门、功能强大,在许多领域中都有广泛的应用,例如最热门的大数据分析,人工智能,Web开发等。
看完上述内容,你们对使用Python怎么在终端输出彩色字符有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。