Define color like this:
W = '\033[0m' # white (normal)
R = '\033[31m' # red
G = '\033[32m' # green
O = '\033[33m' # orange
B = '\033[34m' # blue
P = '\033[35m' # purple
print(R+"hello how are you"+W)
Demo:

see all color codes here:Color Codes
Answer from Hackaholic on Stack OverflowDefine color like this:
W = '\033[0m' # white (normal)
R = '\033[31m' # red
G = '\033[32m' # green
O = '\033[33m' # orange
B = '\033[34m' # blue
P = '\033[35m' # purple
print(R+"hello how are you"+W)
Demo:

see all color codes here:Color Codes
Below is a handy function I find useful. It will print the text you provide in the desired foreground and background colors you specify using standard RGB tuples so you do not have to remember ANSI codes. To find the RGB values you may want to use you can use the color picker at https://www.w3schools.com/colors/colors_picker.asp.
def print_in_color(txt_msg,fore_tupple,back_tupple,):
#prints the text_msg in the foreground color specified by fore_tupple with the background specified by back_tupple
#text_msg is the text, fore_tupple is foregroud color tupple (r,g,b), back_tupple is background tupple (r,g,b)
rf,gf,bf=fore_tupple
rb,gb,bb=back_tupple
msg='{0}' + txt_msg
mat='\33[38;2;' + str(rf) +';' + str(gf) + ';' + str(bf) + ';48;2;' + str(rb) + ';' +str(gb) + ';' + str(bb) +'m'
print(msg .format(mat))
print('\33[0m') # returns default print color to back to black
# example of use using a message with variables
fore_color='cyan'
back_color='dark green'
msg='foreground color is {0} and the background color is {1}'.format(fore_color, back_color)
print_in_color(msg, (0,255,255),(0,127,127))
This somewhat depends on what platform you are on. The most common way to do this is by printing ANSI escape sequences. For a simple example, here's some Python code from the Blender build scripts:
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
To use code like this, you can do something like:
print(bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC)
Or, with Python 3.6+:
print(f"{bcolors.WARNING}Warning: No active frommets remain. Continue?{bcolors.ENDC}")
This will work on unixes including OS X, Linux and Windows (provided you use ANSICON, or in Windows 10 provided you enable VT100 emulation). There are ANSI codes for setting the color, moving the cursor, and more.
If you are going to get complicated with this (and it sounds like you are if you are writing a game), you should look into the "curses" module, which handles a lot of the complicated parts of this for you. The Python Curses HowTO is a good introduction.
If you are not using extended ASCII (i.e., not on a PC), you are stuck with the ASCII characters below 127, and '#' or '@' is probably your best bet for a block. If you can ensure your terminal is using a IBM extended ASCII character set, you have many more options. Characters 176, 177, 178 and 219 are the "block characters".
Some modern text-based programs, such as "Dwarf Fortress", emulate text mode in a graphical mode, and use images of the classic PC font. You can find some of these bitmaps that you can use on the Dwarf Fortress Wiki see (user-made tilesets).
The Text Mode Demo Contest has more resources for doing graphics in text mode.
There is also the Python termcolor module. Usage is pretty simple:
from termcolor import colored
print(colored('hello', 'red'), colored('world', 'green'))
It may not be sophisticated enough, however, for game programming and the "colored blocks" that you want to do...
To get the ANSI codes working on windows, first run
os.system('color')
Here are a few of the colors I know:
purple = "\033[95m" cyan = "\033[96m" darkcyan = "\033[36m" blue = "\033[94m" green = "\033[92m" yellow = "\033[93m" red = "\033[91m" magenta = "\033[35m" white = "\033[37m" black = "\033[30m"
I cant figure out what the code is for orange.
Thanks in advance!
Escape codes come from the days where a computer couldn't display more than 8 different colours simultaneously - They had to pick what these 8 colours (8 foreground, 8 background for a total of "16 colours") were, and orange was not one of the choices when they selected them.
EDIT: Please note that these are colours defined in the ANSI standard - there are terminals out there that have colours other than ANSI, although ANSI is most widespread (and these days ubiquitous) - Also users may manipulate their terminals to display other colour schemes.
Because you can't just make up a code and assign it a color name because you want it to exist?
Your terminal only has the colors it has available. (Many go to 88 or 256 at this point but those are extended codes.)
Also technically you don't know that 36 (for example) is actually cyan. You just know you are asking for the color in that slot (the terminal can have any color in that slot it wants).