How to Change Text Color in Python

Learn how to use colorama library to print colored text with different colors (such as red, green and blue) in the background and foreground and brightness in Python.
  · 3 min read · Updated may 2021 · General Python Tutorials


Printing to the console in different colors is pretty handy and quite practical, from building fancy scanning scripts to distinguishing different log message types (debug, info, or critical, etc.) in your programs. In this tutorial, you will learn how you can print colored text in Python using colorama library.

We'll be using colorama, let's first install it:

$ pip install colorama

Next, open up a new Python file, and write the following:

from colorama import init, Fore, Back, Style

# essential for Windows environment
init()
# all available foreground colors
FORES = [ Fore.BLACK, Fore.RED, Fore.GREEN, Fore.YELLOW, Fore.BLUE, Fore.MAGENTA, Fore.CYAN, Fore.WHITE ]
# all available background colors
BACKS = [ Back.BLACK, Back.RED, Back.GREEN, Back.YELLOW, Back.BLUE, Back.MAGENTA, Back.CYAN, Back.WHITE ]
# brightness values
BRIGHTNESS = [ Style.DIM, Style.NORMAL, Style.BRIGHT ]

First, we call the init() function, which is necessary on Windows environment for colorama to function properly, it does nothing on other platforms, so you can remove it.

Second, we define all the available foreground colors in FORES list, and background colors in BACKS list, we also define BRIGHTNESS list for different brightness setting.

Next, let's make a function that wraps the regular Python's print() function, but with the ability to set color and brightness:

def print_with_color(s, color=Fore.WHITE, brightness=Style.NORMAL, **kwargs):
    """Utility function wrapping the regular `print()` function 
    but with colors and brightness"""
    print(f"{brightness}{color}{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 and brightness each time we use the function.

We also pass **kwargs so we can use other print() function's keyword arguments, such as end and sep.

Now that we have our function, let's use all foreground colors and print the same text with different colors and each with different brightness:

# printing all available foreground colors with different brightness
for fore in FORES:
    for brightness in BRIGHTNESS:
        print_with_color("Hello world!", color=fore, brightness=brightness)

This will look like in the following image:

Same text with different colors in PythonBlack is not showing, as the terminal's background color is black as well, here is with a different background color:

Same text with different colors in PythonLet's now use background colors:

# printing all available foreground and background colors with different brightness
for fore in FORES:
    for back in BACKS:
        for brightness in BRIGHTNESS:
            print_with_color("A", color=back+fore, brightness=brightness, end=' ')
    print()

You can change the background and the foreground color in the same time, that's why we're iterating over foreground colors too, here's how it'll look:

Different background colors with Colorama in Python

Conclusion

That's it! Now you know all the available foreground and background colors as well as brightness values in colorama library in Python. I hope this was helpful for you to quickly grasp information and copying code for your projects.

Learn also: How to Make a Port Scanner in Python.

Happy coding ♥

View Full Code
Sharing is caring!



Read Also





Comment panel