It is very simple with colorama, just do this:

import colorama
from colorama import Fore, Style
print(Fore.BLUE + "Hello World")

And here is the running result in Python3 REPL:

And call this to reset the color settings:

print(Style.RESET_ALL)

To avoid printing an empty line write this:

print(f"{Fore.BLUE}Hello World{Style.RESET_ALL}")
Answer from Yuchen on Stack Overflow
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')
Discussions

colored printing in python
๐ŸŒ r/learnpython
5
9
February 8, 2022
Color text in python - Stack Overflow
The somewhat random characters ... the next text output to whichever color you pick. You must add the number from the table below. Color Value Default 0 Black 30 Red 31 Green 32 Yellow 33 Blue 34 Purple 35 Cyan 36 White 37 ยท Example: print("Uh, oh, you've been given a", "\033[31m", "warning", ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How can I set up Sublime text to work with python 3.7?
You've got several things going on. First, coloring. Syntax colors depends on the Color Scheme. So make sure that you have that set the way you want it under Preferences >> Color Scheme... which will open up a dialog box and let you select. Secondly, syntax coloring requires setting the syntax correctly. Sublime only know what colors to apply if it knows what languages you're working in. It usually detects when opening a file, however if you're starting at a new buffer, you'll need to tell Sublime what language you're using. You can do that one of two ways. In the lower right corner, there's a little box that will display the current language. On a new buffer it'll say "Plain Text". If you click that it'll pop open a list of languages and you may select "Python" there. Alternatively you can go to View >> Syntax and select from a drop down box there. So that ought to get things looking correct for your language. Finally, as for build systems, I honestly don't know for Python as I don't use build systems for Python. You might be well advised to just use Sublime as a text editor and just open a terminal (command prompt, whatever. If you install Anaconda Python, it's got a shortcut to opening a terminal window where Python interpreter is setup) and run your code there, you'll learn a lot. Just type "python my_new_code.py" or whatever you named the file and away you go. More on reddit.com
๐ŸŒ r/SublimeText
2
4
March 11, 2019
Change font on Spyder 3.2.4

Tools->Preferences gives you the configuration screen, which has a font selector on the General tab.

More on reddit.com
๐ŸŒ r/Python
3
1
December 22, 2017
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ print-colors-python-terminal
Print Colors in Python terminal - GeeksforGeeks
June 27, 2022 - Colorama module is a Cross-platform printing of colored text can then be done using Coloramaโ€™s constant shorthand for ANSI escape sequences: Example 1: Python program to print red text with green background.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ print colored text to terminal with python
Print colored text to terminal with Python | Sentry
We can use these directly, or through a Python library. Terminal output can be colored by surrounding it with ANSI escape sequences: BLACK = '\033[30m' RED = '\033[31m' GREEN = '\033[32m' YELLOW = '\033[33m' # orange on some systems BLUE = '\033[34m' MAGENTA = '\033[35m' CYAN = '\033[36m' LIGHT_GRAY = '\033[37m' DARK_GRAY = '\033[90m' BRIGHT_RED = '\033[91m' BRIGHT_GREEN = '\033[92m' BRIGHT_YELLOW = '\033[93m' BRIGHT_BLUE = '\033[94m' BRIGHT_MAGENTA = '\033[95m' BRIGHT_CYAN = '\033[96m' WHITE = '\033[97m' RESET = '\033[0m' # called to return to standard terminal text color print(BLACK + "black
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-add-colour-to-text-python
How to add colour to text Python? - GeeksforGeeks
January 16, 2023 - To add color and style to text, you should create a class called ANSI, and inside this class, declare the configurations about the text and color with code ANSI.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ colored printing in python
r/learnpython on Reddit: colored printing in python
February 8, 2022 -

Hi, someone has a way to color print in python? Thanks in advance

๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ data science โ€บ python - print colored text with ease
Python - Print colored text with ease | Towards Data Science
October 18, 2021 - These pieces of extra data are ANSI escape character sequences that are used to control cursor location, color and font styling e.g. in terminals. The terminal interprets these sequences as commands, rather than text to just display. In the example below we use Python to tell the terminal to print the text 'This is our message' with white text and a yellow background:
Find elsewhere
๐ŸŒ
ozzmaker.com
ozzmaker.com โ€บ add-colour-to-text-in-python
Add Colour to Text in Python | ozzmaker.com
July 25, 2021 - I am using Spyder (Python 3.7) and none of the ideas are working out. I have tired colorama, termcolor, ANSI escape code method and others too. Can someone please help me out? ... You can use โ€˜ pip install colorama โ€˜ then in the script : โ€œ` from colorama import Fore from colorama import Style #to print red text print(f'{Fore.RED} hi there {Style.RESET_ALL}โ€™) #style reset all โ€œ`
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ how-to-print-colored-text-in-python
How to Print Colored Text in Python
February 27, 2023 - That's not quite right, our cheesy text is spilling over to the next line. We need a reset point to stop the printing of colors. This can be done by appending \033[0;0m to the string as: ... The \033[0;0m code represents a reset pattern that returns the terminal back to its original color scheme. This will provide the following output: Looks much better. Colorama is a Python package that provides methods to print colored text in Python.
๐ŸŒ
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.
๐ŸŒ
Medium
medium.com โ€บ ai-does-it-better โ€บ print-colored-text-in-python-enhance-terminal-output-b90aede058c8
Print Colored Text in Python โ€” Using colorama-termcolor | by Doug Creates | AI Does It Better | Medium
December 28, 2024 - Printing colored text in Python involves using third-party libraries like colorama to manipulate terminal output, enabling developers to enhance readability and differentiate information through colors.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 71521804 โ€บ color-text-in-python
Color text in python - Stack Overflow
The somewhat random characters in the print argument are telling the computer to change the color of the next text output to whichever color you pick. You must add the number from the table below. Color Value Default 0 Black 30 Red 31 Green 32 Yellow 33 Blue 34 Purple 35 Cyan 36 White 37 ยท Example: print("Uh, oh, you've been given a", "\033[31m", "warning", "\033[0m")
๐ŸŒ
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 - There are Python modules especially suited for displaying colored text, ANSI is a no-dependency method used for color codes. ANSI escape sequence is in-band signaling to control locations of cursors and colors, to style the font, and much more. 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")
๐ŸŒ
The Python Code
thepythoncode.com โ€บ article โ€บ change-text-color-in-python
How to Change Text Color in Python - The Python Code
Next, let's make a function that ...lor}{s}{Style.RESET_ALL}", **kwargs) We simply use print() inside, but prepending the text with brightness and color codes and appending Style.RESET_ALL in the end to reset to default color ...
๐ŸŒ
YouTube
youtube.com โ€บ tech with tim
How To Print Colored Text in Python (Colorama Tutorial) - YouTube
In this video I'll be showing how to print colored text in python. This method will show you how to output colored text from python in windows, mac and linux...
Published: December 21, 2020
Views: 128K
๐ŸŒ
Instructables
instructables.com โ€บ design โ€บ software
Printing Colored Text in Python Without Any Module : 3 Steps - Instructables
October 22, 2018 - We see that after the green text is printed, the whole shell changes color! ... It turns all the text white...and a different type of 'dull' white if you see it carefully. If you want to commit your code to GITHUB, then many people using your code may have a customized Python Shell with probably YELLOW or Something as their text/background color!
๐ŸŒ
Vultr
docs.vultr.com โ€บ python โ€บ examples โ€บ print colored text to the terminal
Python Program to print colored text to the terminal
December 26, 2024 - Use the following command: ... Import the colored function from the termcolor module. Apply the colored function to the text you wish to print, specifying the desired color as an argument.
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-36511.html
Colored text
February 27, 2022 - I've been looking on how to change your text in the command when you run python. class Colors: RED = '\x1b[0;31;40' def Starting(): print(Colors.RED + 'Hello') time.sleep(100)This is my code that I thought would work to change my text re...
๐ŸŒ
Kaggle
kaggle.com โ€บ general โ€บ 273188
Add Colour to Text in Python | Kaggle
Color to Text in Python Source: ozzmaker.com To make some of your text more readable, you can use ANSI escape codes to change the colour of the text output i...
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ print colored text to the terminal in python
Print Colored Text to the Terminal in Python - Spark By {Examples}
May 31, 2024 - How to print colored text to the terminal in Python? There are several ways to print colored text to the terminal. Whether you want to use basic ANSI