Print Colors of terminal in Python


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

Updated on: 30-Jul-2019

605 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements