How can I output colored text to the terminal in Python?
I'm responding because I have found out a way to use ANSI codes on Windows 10, so that you can change the colour of text without any modules that aren't built in:
The line that makes this work is os.system("")
, or any other system call, which allows you to print ANSI codes in the Terminal:
import os
os.system("")
# Group of Different functions for different styles
class style():
BLACK = '\033[30m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
WHITE = '\033[37m'
UNDERLINE = '\033[4m'
RESET = '\033[0m'
print(style.YELLOW + "Hello, World!")
Note: Although this gives the same options as other Windows options, Windows does not full support ANSI codes, even with this trick. Not all the text decoration colours work and all the 'bright' colours (Codes 90-97 and 100-107) display the same as the regular colours (Codes 30-37 and 40-47)
Edit: Thanks to @j-l for finding an even shorter method.
tl;dr: Add os.system("")
near the top of your file.
Python Version: 3.6.7