Search code examples
pythoncmdcolorsformat

How to change the color of a certain print line in command prompt with python?


Is there a way to change the color or style of a certain print line in CMD with Python?

I want to change the color of text directly in CMD with python commands.

I've tried :

import colorama
from colorama import Fore

print(Fore.RED + 'This text is red in color')

print("\033[1;32m This text is Bright Green  \n")


from colored import fg
print ('%s Hello World !!! %s' % (fg(1), attr(0)))

but none of it works. They just open a new window for a short period of time.


Solution

  • Try adding the init() command from colorama

    import colorama
    from colorama import Fore, init
    
    init()
    
    print(Fore.RED + 'This text is red in color')
    
    print("\033[1;32mThis text is Bright Green  \n")
    

    Colorama Help

    Then run the python script from the CMD, some thing like this

    C:\...>colouredText.py

    Output

    enter image description here