mlrg43
mlrg43

Reputation: 31

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

Upvotes: 3

Views: 2271

Answers (2)

techm4te
techm4te

Reputation: 41

With Windows try clearing the screen before the print command:

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

Upvotes: 4

Willian Vieira
Willian Vieira

Reputation: 746

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')

Upvotes: 2

Related Questions