# the string ' 33[92m' represents the color green,
# and ' 33[0m' is used to go back to the standard color of the terminal
GREEN = ' 33[92m'
END_COLOR = ' 33[0m'
print(GREEN + "Hello World" + END_COLOR)
class bcolors:
HEADER = ' 33[95m'
OKBLUE = ' 33[94m'
OKCYAN = ' 33[96m'
OKGREEN = ' 33[92m'
WARNING = ' 33[93m'
FAIL = ' 33[91m'
ENDC = ' 33[0m'
BOLD = ' 33[1m'
UNDERLINE = ' 33[4m'
print(bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC)
# OR
print(f"{bcolors.WARNING}Warning: No active frommets remain. Continue?{bcolors.ENDC}")
"""
Let’s start by installing the library:
$ pip install colorama
CHANGING COLOR
We can start by changing the color of the text.
Colorama allows you to use eight different colors:
black, red, green, yellow, blue, magenta, cyan, white.
They are implemented as variables in the Fore class.
Their name is the name of the color, written all upper case
"""
from colorama import Fore, init
init()
print('Now is not colored')
print(Fore.RED + 'some red text')
print(Fore.GREEN + 'some green text')
print(Fore.MAGENTA + 'some magenta text')
print(Fore.RESET + 'Back to normal')
"""
CHANGING BACKGROUND
The next class we will see is Back .
This implements the same keyword as the Fore class:
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
However in this case the color will be used to change the background of the string
(i.e. to highlight the text)
"""
from colorama import Back, init
init()
print('Now is not highlighted')
print(Back.RED + 'some red background')
print(Back.GREEN + 'some green background')
print(Back.MAGENTA + 'some magenta background')
print(Back.RESET + 'Back to normal')
"""
CHAANGING BRIGHTNESS
we can use the Style class to change the brightness of the output.
There are three keywords in this class:
BRIGHT make the text bright;
DIM make the text dim (although it looks the same as normal text);
NORMAL to have the normal brightness.
"""
from colorama import Style, init
init()
print('Normal text')
print(Style.BRIGHT + 'Bright text')
print(Style.NORMAL + 'Normal text')
# this class also implements the RESET_ALL keyword,
# which is used to reset everything (brightness, color, background) to their normal values
from colorama import Fore, Back, Style, init
init()
print(Style.BRIGHT + 'Now the text is bright')
print(Fore.RED + 'Now the text is bright and red')
print(Back.GREEN + 'Now the text is bright, red and with green background')
print(Style.RESET_ALL + 'Now everything is back to normal')