Home > database >  Change color text in windows cmd not working
Change color text in windows cmd not working

Time:01-24

I want to change color of text. And when I try it in VS code it works. But when I try it in CMD in Windows I get this:

←[91mstring

Code in python:

print ('\033[91m'   "string")

CodePudding user response:

There is a "bug" in cmd. You need to first clear the screen with:

import os
os.system("cls")

Than it should work.

Full code:

import os
os.system("cls")

print('\033[91m'   "string")

I hope my answer will help.

CodePudding user response:

This is how to use it properly :

import os
os.system("cls")

print('\033[91m'   "string")

There is also the Python termcolor module. Usage is pretty simple:

from termcolor import colored

print(colored('string', 'red'), colored('string', 'green'))
  •  Tags:  
  • Related