How to Print Bold Text in Python

Here are five ways to print bold text in Python.

  1. Using ANSI escape
  2. Using the termcolor
  3. Using the color class
  4. Using the Colorama package
  5. Using Prompt_toolkit package

Method 1: Using ANSI escape

To print bold text in Python, you can use the built-in “ANSI escape sequences” to make text bold, italic, or colored. The text can be printed using the particular ANSI escape sequences in different formats.

The ANSI escape sequence to print bold text in Python is: ‘\033[1m’.

print("This is bold text looks like:",'\033[1m' + 'Python' + '\033[0m')

Output

Print Bold Python Text

You can see from the output that Python is bold. Although, my console is zsh. So it displays white color. But you can think of it as bold text.

Method 2: Using the termcolor

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

We first install the termcolor module.

Next, we use pip to install packages in Python.

python3 -m pip install termcolor

Now, let’s write the colored text.

from termcolor import colored

print(colored('python', 'red', attrs=['bold']))

Output

python

You can count the above text as red-colored text in the output.

Method 3: Using the color Class

In this approach, we will create a color class. Then, the ANSI escape sequence of all the colors is listed in the class. Then, to print the color of our choice, we can select any colors.

class 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.BLUE + 'Python 3!')

Output

Using the color Class

Method 4: Using the Colorama package

To work with the Colorama package, you need to install the package.

python3 -m pip install colorama

It is a cross-platform for colored terminal text. In addition, it makes ANSI works under Microsoft Windows for escape character sequences.

from colorama import init
from termcolor import colored

init()
print(colored('Python 3 !', 'green', 'on_red'))

Output

Using the colorama package

We used a 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. In addition, the colored() function will color the specified string green.

Method 5: Using Prompt_toolkit package

Prompt_toolkit includes a print_formatted_text() function that is compatible (as much as possible) with the built-in function. It also supports colors and formatting.

from prompt_toolkit import print_formatted_text, HTML

print_formatted_text(HTML('<b>The text is bold</b>'))
print_formatted_text(HTML('<i>The text is italic</i>'))
print_formatted_text(HTML('<u>The text is underlined</u>'))

Output

Using Prompt_toolkit package

That’s it.