How to Print Bold Text in Python

Here are five ways to print bold text in Python:

  1. Using ANSI escape
  2. Using termcolor module
  3. Using simple_color package
  4. Using Colorama package
  5. Using Prompt_toolkit package

Method 1: Using ANSI escape

You can make text appear bold in the output by using ANSI escape sequences.

Visual Representation

Visual Representation of Python Print Bold Text Using ANSI escape

Example

# Define the ANSI escape sequences for bold text formatting
BOLD = '\033[1m'
RESET = '\033[0m'

# Print normal text
print("This is not a bold text")

# Print text in bold format
print(BOLD + "This is a bold text" + RESET)

Output

Using ANSI escape

Method 2: Using termcolor module

You can use the termcolor module to print text in the terminal with various attributes.

If you want to print text without any special attributes (such as bold), you simply do not need to specify the attrs argument.

Next, we use pip to install package using pip:

python3 -m pip install termcolor

Visual Representation

Visual Representation of Using termcolor module

Example

from termcolor import colored

# Print text without any special attributes
print(colored("This is not a bold text"))

# Print bold green text
print(colored("This is a bold text", 'green', attrs=['bold']))

Output

Using termcolor module

Method 3: Using simple_color package

The simple-colors package provides functionalities for colorizing text in the terminal.

You can also format your text in various styles, such as bold, italic, underlined, among others which is included in the package.

You can install simple_color package using pip:

python -m pip install simple-colors

Visual Representation

Visual Representation of Using simple_color package

Example

from simple_colors import *

# Print normal green text
print(green("This is not a bold text"))

# Print bold green text
print(green("This is a bold text", 'bold'))

Output

Using simple_color Package

Method 4: Using Colorama package

The colorama package is a popular choice for adding color and style to text in terminal output.

You can install Colorama package using pip:

python3 -m pip install colorama

Visual Representation

Visual Representation of Using Colorama package

Example

from colorama import init, Style
init()

# Print normal text
print("This is not a bold text")

# Print text in bold
print(Style.BRIGHT + "This is a bold text" + Style.RESET_ALL)

Output

Using Colorama package

Method 5: Using Prompt_toolkit package

You can use print_formatted_text() from the prompt_toolkit package along with HTML to format strings.

First, you need to install the prompt_toolkit library. You can do this using pip:

python3 -m pip install prompt-toolkit

Visual Representation

Visual Representation of Using Prompt_toolkit package

Example

from prompt_toolkit import print_formatted_text, HTML

# Printing normal text
print_formatted_text("This is not bold text")

# Printing bold text using HTML formatting
print_formatted_text(HTML('<b>This is bold text</b>'))

Output

Output of Using Prompt_toolkit package

That’s it.