-
-
Save pharaoh1/856975e3a2fbc6d053ee8747b747276b to your computer and use it in GitHub Desktop.
Python ANSI Colors Tutorial example
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 os | |
os.system("cls") #use this for windows. change to os.system("clear") for linux | |
COLORS = {\ | |
"black":"\u001b[30;1m", | |
"red": "\u001b[31;1m", | |
"green":"\u001b[32m", | |
"yellow":"\u001b[33;1m", | |
"blue":"\u001b[34;1m", | |
"magenta":"\u001b[35m", | |
"cyan": "\u001b[36m", | |
"white":"\u001b[37m", | |
"yellow-background":"\u001b[43m", | |
"black-background":"\u001b[40m", | |
"cyan-background":"\u001b[46;1m", | |
} | |
#You can add more colors and backgrounds to the dictionary if you like. | |
def colorText(text): | |
for color in COLORS: | |
text = text.replace("[[" + color + "]]", COLORS[color]) | |
return text | |
#Example printing out some text | |
hello = "[[red]]hello [[blue]]world[[white]]" | |
print(colorText(hello)) | |
#Example printing out an ASCII file | |
f = open("pythonlogo2.txt","r") | |
ascii = "".join(f.readlines()) | |
print(colorText(ascii)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment