【发布时间】:2020-05-28 09:59:39
【问题描述】:
我有可以打印到屏幕上的 python 脚本,有时我希望它们是粗体或绿色,有时是粗体和绿色。但我似乎无法弄清楚如何做到这两点。
class colortext():
def __init__(self, text:str):
self.text = text
self.ending = '\033[0m'
def bold(self):
return '\033[1m' + self.text + self.ending
def green(self):
return '\033[92m' + self.text + self.ending
print(colortext('hello').bold().green())
AttributeError: 'str' object has no attribute 'green'
【问题讨论】:
-
它们不是
bash颜色;它们是终端处理的 ANSI 转义序列。
标签: python string class printing colors