Skip to content

Instantly share code, notes, and snippets.

@yihyang
Last active July 3, 2017 06:07
Show Gist options
  • Select an option

  • Save yihyang/83dcef8ad5c33fb701745476f4cb3f2b to your computer and use it in GitHub Desktop.

Select an option

Save yihyang/83dcef8ad5c33fb701745476f4cb3f2b to your computer and use it in GitHub Desktop.
Python Colored Terminal Output

Terminal Color

Having python to print output to terminals with different colors.

Execute

  • Place both files side by side and execute python test_print.py to see the result

Environment

  • Target version: 3.5 and above

Reference:

# targeted environment: Python 3.5
# Inspired by: https://stackoverflow.com/a/287944
class bcolors:
MAGENTA = '\033[95m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# targeted environment: Python 3.5
# import module
from terminal_color import bcolors
print(bcolor.RED + "This is a red color message." + bcolor.ENDC)
print(bcolor.GREEN + "This is a green color message." + bcolor.ENDC)
print(bcolor.BLUE + "This is a blue color message." + bcolor.ENDC)
print(bcolor.MAGENTA + "This is a magenta color message." + bcolor.ENDC)
print(bcolor.YELLOW + "This is a yellow color message." + bcolor.ENDC)
print(bcolor.BOLD + "This is a bold message." + bcolor.ENDC)
print(bcolor.UNDERLINE + "This is an underlined message." + bcolor.ENDC)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment