Python 更改背景颜色和文本颜色

Python 更改背景颜色和文本颜色,python,python-3.x,colors,Python,Python 3.x,Colors,假设我在列表中有几个数字: listt = [2, 4, 6] 有没有办法根据数字的不同来改变背景色和每个数字的颜色。例如: for i in range(3): if listt[i] == 2: # make background color green and make number red elif listt[i] == 4: # make background color orange and make number green

假设我在列表中有几个数字:

listt = [2, 4, 6]
有没有办法根据数字的不同来改变背景色和每个数字的颜色。例如:

for i in range(3):
    if listt[i] == 2:
        # make background color green and make number red
    elif listt[i] == 4:
        # make background color orange and make number green
    elif listt[i] == 6
        # make background color red and make number orange
    print(nlistt[i])

有什么方法可以做到这一点,如果不同时使用背景色和常规色,您可以选择其中的一种。此外,这应该在控制台中,而不是在pygame这样的新窗口中。

只需将颜色代码应用于您想要打印的内容,颜色代码可以在@Alex Taylor的帖子中找到


可能重复它不是重复的,我不知道如何根据列表中的项目更改颜色您希望它出现在新窗口(例如pygame、pyglet)还是控制台中?我希望它出现在console@J.Doe你可以根据alex的建议。。。它就在那里,至少有57张选票,如何用彩色印刷,实际上是用背景
listt = [2, 4, 6]
nlistt = listt.copy()
for i in range(3):
    if listt[i] == 2:
        # make background color green and make number red
        nlistt[i] = '\033[1;31;42m' + str(nlistt[i]) + '\033[0m'
    elif listt[i] == 4:
        # make background color orange and make number green
        nlistt[i] = '\033[1;32;43m' + str(nlistt[i]) + '\033[0m'
    elif listt[i] == 6:
        # make background color red and make number orange
        nlistt[i] = '\033[1;33;41m' + str(nlistt[i]) + '\033[0m'
    print(nlistt[i])