Add text color while print - Python

102 views Asked by At

I want to add text color while print output. How to do that ?

Example: Print('Hello Everyone.') I want output text Hello Everyone will highlighted red color.

Thanks you!

2

There are 2 answers

2
WangGang On BEST ANSWER

An easy way to do this is to import a color module like colorama or termcolor. This site might be useful: Print Colors in Python terminal. For you code it would be:

from colorama import Fore
print(Fore.RED + 'Hello Everyone') 

or

import sys 
from termcolor import colored, cprint
print(colored('Hello Everyone', 'red'))  # method one
cprint('Hello Everyone', 'red')          # method two
2
krishna On

Try this.

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

print(bcolors.OKGREEN + 'Hello Everyone')