温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Python怎么在终端输出彩色字符

发布时间:2021-05-21 18:06:13 来源:亿速云 阅读:242 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关使用Python怎么在终端输出彩色字符,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

一.Colored版本

1:采用原生的转义字符序列---对windows有的版本不支持(比如win7),linux完美支持

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

颜色对比图(根据需要自己设置对应的值):

使用Python怎么在终端输出彩色字符

运行效果:

使用Python怎么在终端输出彩色字符

二.Colored版本

2:采用python标准库的colorama模块--兼容linux和windows各个版本:

# -----------------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可以做什么

Python是一种编程语言,内置了许多有效的工具,Python几乎无所不能,该语言通俗易懂、容易入门、功能强大,在许多领域中都有广泛的应用,例如最热门的大数据分析,人工智能,Web开发等。

看完上述内容,你们对使用Python怎么在终端输出彩色字符有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI