简体   繁体   中英

Change console print color in python 3.8 with ANSI escape codes (Windows)

I'm trying to print colored code in the python shell with ANSI escape codes. My code looks like this:

print("\033[031m" + "Hello" + "\033[0m")

When I run the code in Visual Studio Code it works perfectly fine but if I open it directly in Python 3.8 my output is: [031mHello[0m

With Windows try clearing the screen before the print command:

import os
os.system("cls")
print("\033[031m" + "Hello" + "\033[0m")

try to use colorama:

from colorama import Fore, Back, Style
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM