Skip to content

Instantly share code, notes, and snippets.

@J-Yaghoubi
Last active September 2, 2022 02:16
Show Gist options
  • Select an option

  • Save J-Yaghoubi/0e1aa749d8ccadb58cab4cc3f869efaf to your computer and use it in GitHub Desktop.

Select an option

Save J-Yaghoubi/0e1aa749d8ccadb58cab4cc3f869efaf to your computer and use it in GitHub Desktop.
How to use ANSI-escape in python to have colored font and background
import sys
# 256-colored Text
for i in range(0, 16):
for j in range(0, 16):
code = str(i * 16 + j)
sys.stdout.write(u"\u001b[38;5;" + code + "m " + code.ljust(4))
print (u"\u001b[0m")
# 256-colored backgrounds
for i in range(0, 16):
for j in range(0, 16):
code = str(i * 16 + j)
sys.stdout.write(u"\u001b[48;5;" + code + "m " + code.ljust(4))
print (u"\u001b[0m")
# Example:
print(u"\u001b[38;5;32m Colored Text")
print(u"\u001b[48;5;131m Colored Background")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment