Search code examples
pythonwindowsterminal-emulator

Change Color Output in Python in windows


I'm trying to change the output text to green and the background to blue using this code:

print("\033[1;32;44m******************************************************\n")
print("\033[1;32;44m* Terminate Instance Operations in AWS Are Complete. *\n")
print("\033[1;32;44m******************************************************\n")

But the output is the the default powershell white and the numeric codes are visible. This is how the output looks:

[1;32;44m******************************************************

[1;32;44m* Terminate Instance Operations in AWS Are Complete. *

[1;32;44m******************************************************

I'm running the script on win 10 currently. What am I doing wrong?


Solution

  • Depending on your version of Windows 10, your console host may not support VTY emulation which enables you to use those escape sequences. You can work around this as such:

    import os
    
    os.system('COLOR 12')
    

    See cmd.exe /c COLOR /? for more.


    To see if your console has the support, enter this command in PowerShell:

    $Host.UI.SupportsVirtualTerminal