Skip to content

Instantly share code, notes, and snippets.

@hasanisaeed
Created December 29, 2022 12:13
Show Gist options
  • Select an option

  • Save hasanisaeed/36e69933b8a55414f123e0fcf2b7aff9 to your computer and use it in GitHub Desktop.

Select an option

Save hasanisaeed/36e69933b8a55414f123e0fcf2b7aff9 to your computer and use it in GitHub Desktop.
Color printing in the terminal!
NO_COLOR = "0" # Text Reset
BLACK = "0;30" # Black
RED = "0;31" # Red
GREEN = "0;32" # Green
YELLOW = "0;33" # Yellow
BLUE = "0;34" # Blue
PURPLE = "0;35" # Purple
CYAN = "0;36" # Cyan
WHITE = "0;37" # White
def print_colored(text, color: str = "0"):
print(f"\033[{color}m{text}\033[0m")
# ---------- Examples ----------
print_colored(">> Hello World with NO_COLOR", NO_COLOR)
print_colored(">> Hello World with BLACK", BLACK)
print_colored(">> Hello World with RED", RED)
print_colored(">> Hello World with GREEN", GREEN)
print_colored(">> Hello World with YELLOW", YELLOW)
print_colored(">> Hello World with BLUE", BLUE)
print_colored(">> Hello World with PURPLE", PURPLE)
print_colored(">> Hello World with CYAN", CYAN)
print_colored(">> Hello World with WHITE", WHITE)
@hasanisaeed
Copy link
Copy Markdown
Author

Output:

Screenshot from 2022-12-29 15-41-31

@hasanisaeed
Copy link
Copy Markdown
Author

If you want more colors, visit this page!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment