python color console output windows code example

Example 1: python color in console

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(f"{bcolors.WARNING}Error : Test message !{bcolors.ENDC}")

Example 2: python color text on windows

import termcolor
import os
os.system('color')
print(termcolor.colored("I want to help", "red"))

Example 3: how to change the color of command prompt in python

import os
# To get all possible colors for the command line, open the command prompt
# and enter the command "color help"
os.system('color FF')

Example 4: how to use colorama

# Colorama is a module to color the python outputs
# 1. You have to install python with pip
# 2. go to your commandline and type:
#	linux: sudo pip install colorama
#	windows + mac: pip install colorama
# 3. wait for the download and then create a new python file
# 4. use the module colorama
import colorama
from colorama import Fore, Back, Style
# 5. the best way is to use colorama with f-strings
colorama.init(autoreset=True)#auto resets your settings after every output

print(f"{Fore.GREEN}green is one of the colors, there are many other colors!")

Example 5: print colored text to console python

colored = lambda r, g, b, txt : f"\033[38;2;{r};{g};{b}m{txt}\033[m"