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.

Answer from Gugu72 on Stack Overflow
🌐
Fabian Lee
fabianlee.org › 2021 › 12 › 03 › python-printing-in-color-using-ansi-color-codes
Python: printing in color using ANSI color codes | Fabian Lee : Software Engineer
December 5, 2021 - Although there are Python modules ... an example of printing a line in green, then red. print("\033[0;32mOK this is green\033[00m") print("\033[0;31mERROR this is red\033[00m") Additional color codes can ......
🌐
GitHub
gist.github.com › rene-d › 9e584a7dd2935d0f461904b9f2950007
ANSI color codes in Python · GitHub
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 ... I recommend you to use such a script to find the color code you want: https://gist.github.com/b5370efb61db005d7de0ad47959cd46c · Depends on your terminal, you can also use RGB : https://en.wikipedia.org/wiki/ANSI_escape_code
Discussions

shell - Change color of output using Python ANSI codes for ONLY one line? - Stack Overflow
I have a Python script with several outputs in the case of an error, and if the error occurs then I want the message to be printed in red. I used ANSI codes to do this, such as in the example below... More on stackoverflow.com
🌐 stackoverflow.com
python - How do I print colored text to the terminal? - Stack Overflow
Although there are many simple libraries available for this task (colorama, colorit, colorist , rich etc) , I prefer/suggest directly using ANSI codes in your codebase . Please refer python colored printing script that is very straight forward & easy to use. You can directly copy-paste / fork ... More on stackoverflow.com
🌐 stackoverflow.com
How do I make the output text orange using ANSI code?
orangeBg = "\033[48;2;255;165;0m" Source: https://stackoverflow.com/questions/32573654/is-there-a-way-to-create-an-orange-color-from-ansi-escape-characters This should help you. More on reddit.com
🌐 r/learnpython
2
3
June 27, 2018
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')
2

If you want to know what those characters mean, check out the wikipedia page.

More on reddit.com
🌐 r/learnpython
9
4
December 27, 2017
People also ask

How do I print a solid colored block character in Python
ANS: Use the Unicode character for a full block, such as \u2588, and wrap it with the appropriate ANSI escape codes for background or foreground color, for example: \033[42m\u2588\033[0m for a green background block.
🌐
sqlpey.com
sqlpey.com › python › python-terminal-text-coloring-methods
Python Terminal Text Coloring Techniques: ANSI Escape Codes and ...
Does direct ANSI code printing work on Windows terminals
ANS: Direct ANSI code printing often requires Windows 10 with VT100 emulation enabled, or using a utility like os.system('color') beforehand to activate the support layer. Libraries like colorama automate this compatibility.
🌐
sqlpey.com
sqlpey.com › python › python-terminal-text-coloring-methods
Python Terminal Text Coloring Techniques: ANSI Escape Codes and ...
What is the recommended Python package for modern console output
ANS: The Rich library is highly recommended for its BBCode-like syntax, robust feature set, and handling of complex terminal interactions.
🌐
sqlpey.com
sqlpey.com › python › python-terminal-text-coloring-methods
Python Terminal Text Coloring Techniques: ANSI Escape Codes and ...
🌐
PyPI
pypi.org › project › ansicolors
ansicolors · PyPI
June 2, 2017 - Even deep into the Web era, text output endures as an important means of human-computer interaction. The good news, such is it is: ANSI’s color and style specifications (“SGR” or “Select Graphic Rendition” in ANSI terminology) are the most-used and best-adhered-to portion of the whole ANSI show. # use some partial functions from __future__ import print_function # so works on Python 2 and 3 alike from colors import red, green, blue print(red('This is red')) print(green('This is green')) print(blue('This is blue'))
🌐
PyLand
pylandschool.com › en › blog › article › ansi-color-table
ANSI Color Table for Python 🎨 - PyLand Blog
July 13, 2026 - Store the codes in variables and insert them into an f-string: RED = '\033[31m' RESET = '\033[0m' print(f'{RED}Red text{RESET}') print('Normal text') The \033 sequence represents the special Escape character.
🌐
Sentry
sentry.io › sentry answers › python › print colored text to terminal with python
Print colored text to terminal with Python | Sentry
How can I print colored text to the terminal in Python? On most platforms, terminal colors are produced using ANSI escape sequences.
Top answer
1 of 16
3045

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.

2 of 16
1220

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')
Find elsewhere
🌐
Jakob-bagterp
jakob-bagterp.github.io › colorist-for-python › ansi-escape-codes › introduction
Ultimate Guide to ANSI Escape Codes - Colorist for Python 🌈
We simply wrap the word RED with the ANSI escape sequences \x1b[31m to set the color and \x1b[0m to reset the color. The terminal will then interpret this as a single command while the characters of the escape sequences are hidden in the terminal output.
🌐
Jakob-bagterp
jakob-bagterp.github.io › colorist-for-python › ansi-escape-codes › standard-16-colors
How to Use Colors in ANSI Escape Codes - Colorist for Python 🌈
To apply different color and styling options, simply replace the two underscores __ in \x1b[__m with any of the following color codes: ... Remember to use \x1b[0m every time you want to revert back to the default terminal text style. Otherwise, any color or styling may spill over and into other terminal messages. When using Colorist, you can for example use Color.OFF to reset the terminal text style. ... % This is RED text % This is RED background % This is BRIGHT RED text % This is BRIGHT RED background ... Instead of using raw ANSI escape codes, it's convenient to use Colorist to generate the them while keeping the code readable.
🌐
sqlpey
sqlpey.com › python › python-terminal-text-coloring-methods
Python Terminal Text Coloring Techniques: ANSI Escape Codes and Libraries
October 29, 2025 - colorama is popular for enabling ANSI color support reliably across different platforms, especially older Windows versions, by wrapping sys.stdout. It supports Python 3.5+ and 2.7. from colorama import init as colorama_init, Fore, Style # Initializes colorama to enable ANSI codes on Windows colorama_init() # Usage example print(f"This text is {Fore.GREEN}colorful{Style.RESET_ALL}!")
🌐
GeeksforGeeks
geeksforgeeks.org › python › print-colors-python-terminal
Print Colors in Python terminal - GeeksforGeeks
June 20, 2025 - The colorama module enables cross-platform support for printing colored text in the terminal using ANSI escape sequences. It works well on Windows (which doesn’t support ANSI codes by default) and wraps the codes in a way that's consistent ...
🌐
Stack Abuse
stackabuse.com › how-to-print-colored-text-in-python
How to Print Colored Text in Python
February 27, 2023 - This is followed by the color code ranging from 0 to 255. Based on the above syntax, let's try recreating the StackAbuse logo in Python using an ANSI Escape Sequence. The logo contains a pale gray background (\033[48;5;236m) with the words: Stack in white (\033[38;5;231m) and Abuse in orange (\033[38;5;208m). And of course, the reset code needs to be embedded in the string. That being said, we can recreate the logo with this ANSI Sequence: >>> print("\033[48;5;236m\033[38;5;231mStack \033[38;5;208mAbuse\033[0;0m")
🌐
Buildsoftwaresystems
buildsoftwaresystems.com › post › colorize-your-terminal-with-ansi-codes
How to Make Your Terminal Talk in Color (with ANSI Codes)
November 3, 2025 - ANSI escape codes colorize and style terminal output. They’re simple text sequences your terminal already understands. They work everywhere — Python, Node, Go, Bash, you name it.
🌐
Finxter
blog.finxter.com › home › learn python blog › how to print colored text in python?
How to Print Colored Text in Python? - Be on the Right Side of Change
May 17, 2022 - Summary: To print colored text in Python, you can use: The simple_color package, The '33[1m' ANSI escape-sequence, The termcolor module, The coloroma package, The colored library, The prompt_toolkit package.
🌐
PyPI
pypi.org › project › ansi
ansi · PyPI
Various ANSI escape codes, used in moving the cursor in a text console or rendering coloured text. Print something in bold yellow on a red background: >>> from ansi.colour import fg, bg >>> from ansi.colour.fx import reset >>> msg = (bg.red, fg.yellow, 'Hello world!', reset) >>> print(''.join(map(str, msg))) ... ... >>> from ansi.colour.rgb import rgb256 >>> from ansi.colour.fx import reset >>> msg = (rgb256(0xff, 0x80, 0x00), 'hello world', reset) >>> print(''.join(map(str, msg))) ... ... Ansi requires python 3.6 and supports typing.
      » pip install ansi
    
Published: Jan 22, 2024
Version: 0.3.7
🌐
AskPython
askpython.com › python › examples › print-colored-text-to-the-terminal-in-python
Print Colored Text to the Terminal in Python - AskPython
February 23, 2023 - Let us take a look at how we can use ANSI to print the colored text to the console. print("\033[0;32m Green Color\n") print("\033[0;34m Blue Color\n") print("\033[1;32;40m Bright Green \n") print("\033[0;31m Red \033[0m\n") In this code, we ...
🌐
Codetrail
codetrail.io › questions › python › print-colored-text-terminal-python
Codetrail
Terminal color output in Python is usually done by printing ANSI escape codes along with your text.
🌐
CodeRivers
coderivers.org › blog › print-with-colors-python
Printing with Colors in Python - CodeRivers
February 22, 2026 - For example, to set the text color to red, the escape code is \033[31m. Here, the number 31 is the parameter that represents the red color in the ANSI color system. The code \033[0m is used to reset the formatting to the default. Python's print function can handle ANSI escape codes.
🌐
Skillsugar
skillsugar.com › how-to-print-coloured-text-in-python
How to Print Coloured Text in Python - SkillSugar
To change the background color of text we can use the ANSI escape sequence character numbers 100-107.
🌐
Lexdsolutions
lexdsolutions.com › post › 2024-12-05-ansi-color
Coloured Text using ANSI Escape Code • Lexd Solutions
December 5, 2024 - To keep things consistent in my scripts, I have opted to use ANSI escape codes which works almost exactly the same for me when writing scripts in both Python or Bash/Shell. ANSI escape sequences were established in the 1970s as a standard for adding color, font styling, and other formatting options to text terminals. Today, they are widely supported by modern terminals on operating systems such as Windows, macOS, and Linux. Here is an example on how I apply colour to my print statements in Python.