Python Print Colored Text | Delft Stack
Maybe your like
- Use ANSI Escape Codes to Print Colored Text in Python
- Use the colorama Module to Print Colored Text in Python
This tutorial shows you how to generate colored text when you print in Python.
The only way to manipulate the command line console using input is by using ANSI Escape Codes. These codes can manipulate console functions, such as text or background color, cursor settings, fonts, and other modifiable elements within the console.
Use ANSI Escape Codes to Print Colored Text in Python
First, let’s declare a Python class that gives us a few ANSI Code that represents colors that we can work with.
class bcolors: OK = "\033[92m" # GREEN WARNING = "\033[93m" # YELLOW FAIL = "\033[91m" # RED RESET = "\033[0m" # RESET COLOR3 of these variables are actual ANSI Code for colors, while the variable RESET is there to set the color back to the default.
The function print() outputs string argument onto the command line console.
If you want the output of print() to be colored, you would have to insert ANSI code within the string that can manipulate the command line console.
Using class bcolors, we’ll print 3 different lines with different colors.
print(bcolors.OK + "File Saved Successfully!" + bcolors.RESET) print(bcolors.WARNING + "Warning: Are you sure you want to continue?" + bcolors.RESET) print(bcolors.FAIL + "Unable to delete record." + bcolors.RESET)We prefix the string with the color you want it to reflect and suffix it with bcolors.RESET to reset the color to default before the next time you use print() or the next time you use the terminal.
If you’re using Python 3, you can also format your print() statement like this:
print(f"{bcolors.OK}File Saved Successfully!{bcolors.RESET}") print(f"{bcolors.WARNING}Warning: Are you sure you want to continue?{bcolors.RESET}") print(f"{bcolors.FAIL}Unable to delete record.{bcolors.RESET}")Output:

After outputting the last line, the terminal will be reset back to its default color because of bcolors.RESET. If you fail to put it at the end of your lines, the text within the terminal will be colored the last color you set within print(). In this case, it would be red.
Use the colorama Module to Print Colored Text in Python
ANSI’s problem is it might not work well with Windows OS, so you would need workarounds to make it work within Windows consoles.
colorama is a Python module that uses ANSI escape codes. This module also makes it possible for ANSI to be compatible with Windows. The documentation explains how they made it possible to wrap ANSI code for Windows compatibility.
Here’s a list of available colorama foreground colors:
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESETThey also have styles, including a RESET_ALL that resets all the current ANSI modifications, while Fore.RESET only resets the latest ANSI.
DIM, NORMAL, BRIGHT, RESET_ALLFirst, we need to install the colorama as it is a community-created library. We can use either pip or anaconda to install this dependency.
- For pip or pip3
- For anaconda
We’ll try some of the colors out and output several lines of different colors and styles. We use colorama.init() to make these settings work for Windows.
import colorama from colorama import Fore from colorama import Style colorama.init() print(Fore.BLUE + Style.BRIGHT + "This is the color of the sky" + Style.RESET_ALL) print(Fore.GREEN + "This is the color of grass" + Style.RESET_ALL) print(Fore.BLUE + Style.DIM + "This is a dimmer version of the sky" + Style.RESET_ALL) print(Fore.YELLOW + "This is the color of the sun" + Style.RESET_ALL)Output:

DIM and BRIGHT will output different shades of the color when used on the same color. RESET_ALL resets the color to default to be set to another color or leave it back to the default.
In summary, the only way to print out colored text in Python is by making use of ANSI Escape Codes. It’s the only way for the console to understand instructions from Python’s print() statement. You can do it manually by including the actual escape code within your print(), or use colorama to wrap the ANSI code into a more readable format.
If you want to get more options for ANSI code colors, there are many resources on the internet to provide them, this article is a good start. You can also format text (bold, underline, italicize), change your console background, and much more with ANSI code.
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. SubscribeTag » Color Printing Python
-
Python - How Do I Print Colored Text To The Terminal? - Stack Overflow
-
Print Colors In Python Terminal - GeeksforGeeks
-
How To Print Colored Text In Python - Stack Abuse
-
How To Print Colored Text In Python - Studytonight
-
Print Colored Text In Python - Linux Hint
-
Add Colour To Text In Python
-
Python — Print Colored Text With Ease - Towards Data Science
-
Python Program To Print Colored Text To The Terminal - Programiz
-
How To Print Colored Text In Python (Colorama Tutorial) - YouTube
-
Print In Color Python Code Example - Code Grepper
-
How To Color Print In Python Code Example
-
How To Print Colored Text In Python? - Finxter
-
How To Print Text In Color Using Python | By Yancy Dennis
-
Python Code Examples For Print Color