Python - How To Print Bold Text in Python

ID : 146

viewed : 246

Tags : PythonPython Print

vote vote


This article will discuss some methods to print bold text in Python.

We can use built-in ANSI escape sequences for making text bold, italic or colored, etc. By using the special ANSI escape sequences, the text can be printed in different formats. The ANSI escape sequence to print bold text is: '\033[1m'. To print the bold text, we use the following statement.

print("The bold text is",'\033[1m' + 'Python' + '\033[0m') 

Here, '\033[0m' ends the bold formatting. If it is not added, the next print statement will keep print the bold text.

This method creates a color class. ANSI escape sequence of all the colors is listed in the class. To print the color of our own choice, we can select any of the colors.

The complete example code is given below.

class bold_color:    PURPLE = '\033[95m'    CYAN = '\033[96m'    DARKCYAN = '\033[36m'    BLUE = '\033[94m'    GREEN = '\033[92m'    YELLOW = '\033[93m'    RED = '\033[91m'    BOLD = '\033[1m'    UNDERLINE = '\033[4m'    END = '\033[0m'  print("The output is:" + color.BOLD + 'Python Programming !' + color.BLUE) 

The termcolor is a package for ANSI color formatting for output in the terminal with different properties for different terminals and certain text properties. We will use bold text attributes in this function. The colored() function gives the text the specific color and makes it bold.

The complete example code is given below.

from termcolor import colored print(colored('python', 'green', attrs=['bold'])) 

It is a cross-platform for colored terminal text. It makes ANSI works under MS Windows for escape character sequences. To use this package, you must install it in your terminal by the following command. If you have not installed it, then the code will not work properly.

pip install colorama conda install -c anaconda colorama 

The complete example code is given below:

from colorama import init from termcolor import colored init() print(colored('Python Programming !', 'green', 'on_red')) 

We use the colorama module with termcolor, to print colored text on the Windows terminal. Calling init() on Windows would filter ANSI escape sequences out of every other text sent to stdout or stderr, replacing them with Win32 equivalent calls. The colored() function will color the specified string in the green color.

We must install this package by the following command.

pip install simple_colours 

It is the simplest method to print bold text in Python.

The complete example code is given below:

from simple_colors import * print(green('Python', 'bold')) 

  • Related HOW TO?