python 命令行常用字体颜色
在平时工作中经常会使用python脚本编写一些程序或工具,经常需要在控制台输出一些信息,例如:请输入用户名、密码等。如果用户名不合法输出‘用户名不规范’等提示信息。但这样输出的字体颜色都是白色,不容易区分重要的提示信息,所以我们可以参考linux命令行的提示。
代码如下,拿走不谢!
class Color(object):
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BlUE = '\033[94m'
END = '\033[0m'
@classmethod
def red(cls, string):
return cls.RED + string + cls.END
@classmethod
def green(cls, string):
return cls.GREEN + string + cls.END
@classmethod
def yellow(cls, string):
return cls.YELLOW + string + cls.END
@classmethod
def cyan(cls, string):
return cls.BlUE + string + cls.END
if __name__ == '__main__':
green = Color.green('this is green, such as ....ok')+' over'
red = Color.red('this is red, such as error:xxxxxx')+' over'
yellow = Color.yellow('this is yellow, such as warning........')+' over'
blue = Color.cyan('this is blue, such as, information')+' over'
print(green)
print(red)
print(yellow)
print(blue)
/