Print Colors of terminal in Python

PythonServer Side ProgrammingProgramming

In the terminal, if you want to make some texts appears in colored mode, there are numerous ways in python programming to achieve it.

Using python modules

1.termcolor module: It is the ANSII Color formatting for output in the terminal.

import sys
from termcolor import colored, cprint
text1 = colored('Hello, Tutorialspoint!', 'blue', attrs=['reverse', 'blink'])
print(text1)
cprint('Hello, Python!', 'blue', 'on_white')
print_red_on_blue = lambda x: cprint(x, 'red', 'on_blue')
print_red_on_blue('Hello, from Data Science!')
print_red_on_blue('Hello, Python!')
for i in range(10):
   cprint(i, 'green', end=' ')
cprint("Attention!", 'blue', attrs=['bold'], file=sys.stderr)

Result

raja
Published on 20-Feb-2019 12:47:19
Advertisements