Print colored text in the terminal in Python
Welcome back, programmers. In this post, we will know-how to print colored text in the terminal in Python.
There are various methods of printing colored text in the terminal and we will learn those methods in this post. So go through this tutorial post to learn the method of printing colorful text in Python terminal or output window.
Various ways in Python to print colored text in the terminal
For printing colorful text in Python terminal, we will use three methods in this post. For this, we will use three different modules:
- termcolor
- colorama
- colored
Method 1: Using termcolor module
Note:- Download the termcolor module before using it in the program[pip install termcolor].
#import colored from termcolor
from termcolor import colored
print(colored('Codespeedy','red'),colored("Technology",'green'))
print(colored('Python Programming Blog','blue'))
Output:
Codespeedy Technology
Python Programming LanguageMethod 2: Using colorama module
from colorama import Fore
print(f"{Fore.BLUE}Welcome to {Fore.MAGENTA}India")
print(f"{Fore.CYAN}Colored text in Python")Output:
Welcome to India
Colored text in PythonMethod 3: Using the colored module
Note:- Download the colored module before using it in the program[pip install colored].
In the above two modules, the variation in colors is limited. But in the colored module, we have 256 different colors, with 13 attributes like bold, italic, etc. For details(fg, bg, attr numbers) prefer the Python documentation.
from colored import fg, bg, attr
#fg=foreground, bg= background, attr= attributes
print("%s Codespeedy technology %s"%(fg(29),attr(0)))
print("%s%sCodespeedy technology %s"%(fg(1),bg(14),attr(21)))
Output:
Codespeedy Technology
Codespeedy TechnologyI hope you understood the concept of printing colored text in Python. If you have any doubts or find anything wrong in this post please comment below.
Also read: Convert RGB to hex color code in Python
Thank You.
Leave a Reply