tags: #python #colour #color #text See e.g. [studytonight](https://www.studytonight.com/python-howtos/how-to-print-colored-text-in-python) and [geeksforgeeks](https://www.geeksforgeeks.org/print-colors-python-terminal/) #### colorama ```python import colorama from colorama import Fore print(Fore.RED + 'This text is red in color') ``` ```python from colorama import Fore, Back, Style print(Fore.RED + 'some red text') print(Back.GREEN + 'and with a green background') print(Style.DIM + 'and in dim text') print(Style.RESET_ALL) print('back to normal now') ``` #### termcolor ```python import sys from termcolor import colored, cprint text = colored('Hello, World!', 'red', attrs=['reverse', 'blink']) print(text) ``` ```python from colorama import init from termcolor import colored init() print(colored('Hello, World!', 'green', 'on_red')) ``` ```python import sys from termcolor import colored, cprint text = colored('Hello, World!', 'red', attrs=['reverse', 'blink']) print(text) cprint('Hello, World!', 'green', 'on_red') def print_red_on_cyan(x): return cprint(x, 'red', 'on_cyan') print_red_on_cyan('Hello, World!') print_red_on_cyan('Hello, Universe!') for i in range(10): cprint(i, 'magenta', end=' ') cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr) ``` #### colored ```python from colored import fg print ('%s Hello World !!! %s' % (fg(1), attr(0))) ``` ### ANSI The old school approach. ```python def prRed(skk): print("\033[91m {}\033[00m" .format(skk)) def prGreen(skk): print("\033[92m {}\033[00m" .format(skk)) def prYellow(skk): print("\033[93m {}\033[00m" .format(skk)) def prLightPurple(skk): print("\033[94m {}\033[00m" .format(skk)) def prPurple(skk): print("\033[95m {}\033[00m" .format(skk)) def prCyan(skk): print("\033[96m {}\033[00m" .format(skk)) def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk)) def prBlack(skk): print("\033[98m {}\033[00m" .format(skk)) prCyan("Hello World, ") prYellow("It's") prGreen("Geeks") prRed("For") prGreen("Geeks") ```