Last active
September 2, 2022 02:16
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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