Cannot print colored text on Windows

Issue

I’m new to Python, trying to print a colored text in the console. My OS is windows 10. Here is the code:

class bcolors:
    """
    Colors for console
    """
    HEADER = '\033[95m'
    YGREEN = '\033[92m'
    WARNING = '\033[93m'
    ENDC = '\033[0m'

def main():
    user_name: str = input(
        f'***Hello {bcolors.YGREEN}friend{bcolors.ENDC} Welcome***\nPlease enter your Name:\n')

if __name__ == '__main__':
    main()

but when I execute this code either through CMD or PyCharm, I don’t get colored text. Here is the output:

pic

What’s wrong?

Solution

This code only works on an XTerm-compliant Linux/Unix terminal. If you would like this code to work, see https://pypi.org/project/colorama.

import colorama
colorama.init(wrap=True)

class bcolors:
    """
    Colors for console
    """
    HEADER = '\033[95m'
    YGREEN = '\033[92m'
    WARNING = '\033[93m'
    ENDC = '\033[0m'

def main():
    print(f'***Hello {bcolors.YGREEN}friend{bcolors.ENDC} Welcome***\nPlease enter your Name:\n', end='')
    user_name: str = input()

if __name__ == '__main__':
    main()

Answered By – not my real name

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published