windows cmd 中的更改颜色文本不起作用

Change color text in windows cmd not working

我想改变文字的颜色。当我在 VS 代码中尝试它时,它起作用了。 但是当我在 Windows 中的 CMD 中尝试它时,我得到了这个:

←[91mstring

python中的代码:

print ('3[91m' + "string")

cmd 中存在“错误”。您需要先使用以下命令清除屏幕:

import os
os.system("cls")

比它应该工作。

完整代码:

import os
os.system("cls")

print('3[91m' + "string")

希望我的回答对您有所帮助。

正确使用方法如下:

import os
os.system("cls")

print('3[91m' + "string")

还有Python termcolor module。用法很简单:

from termcolor import colored

print(colored('string', 'red'), colored('string', 'green'))