You need to use the RESET ANSI code to reset applied style:
print(f"\033[1;31;40m An error occurred while verifying alerts: {str(e)}\033[0m")
I often use this cheatsheet for ANSI codes, the ESCAPE sequence is \033 in Python.
shell - Change color of output using Python ANSI codes for ONLY one line? - Stack Overflow
python - How do I print colored text to the terminal? - Stack Overflow
How do I make the output text orange using ANSI code?
Python and ANSI-escape sequences?
When you want to write a character that is not printable, you write \x followed by the number in hexadecimal format (2 characters in 0-9 or a-f). So \x1b is a single character to python.
Your examples are 2 characters: \x1b and 7. You can see this in the REPL:
>>> len('\x1b7')
2If you want to know what those characters mean, check out the wikipedia page.
More on reddit.comHow do I print a solid colored block character in Python
Does direct ANSI code printing work on Windows terminals
What is the recommended Python package for modern console output
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')
» pip install ansi