Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Printing Bold Text in Python

  • 08 Apr, 2025
  • Com 0
Printing bold text in Python

For drawing the user’s attention to key information or highlighting important messages, bolding some words or phrases can breakup the monotony and improve user’s readability.

To print bold text, it depends on the output environment (e.g., terminal, HTML, GUI).

Terminal / Console output

No matter if you are using Windows, Mac, or Linux, most terminals support ANSI escape codes.

You can enable bold text with \033[1m and reset with \033[0m.

# 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)

Bold text output in terminal

The above output image shows the bold text in the macOS terminal.

If by default, ANSI support is not enabled, you can enable it using the code below:

import os

os.system("")  # Enables ANSI codes in some terminals

print("\033[1mBold Text\033[0m")

Using the Colorama package

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

You can install the Colorama package using pip by the command below:

pip install colorama

Here is the full code:

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 using colorama" + Style.RESET_ALL)

Output using colorama package

Using the termcolor module

You can use the termcolor module to print text in the terminal with various attributes. 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 the package:

pip install termcolor

diagram of using termcolor module

 

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 of using the termcolor module

Web / HTML Output

If you are using web frameworks like Django or Flask, you can use HTML and CSS to bold the text that will be displayed as a webpage in the browser.

# Example for HTML generation

html_content = "<b>Bold Text</b>"  # or <strong>Bold Text</strong>

html_content_css = "<span style='font-weight: bold;'>Bold Text</span>"

GUI Applications

If you are working on a GUI application, you must use the tkinter library. You can handle the Bold text via widget styling.

import tkinter as tk

root = tk.Tk()

label = tk.Label(root, text="Bold Text", font=("Arial", 12, "bold"))

label.pack()

root.mainloop()

Output of GUI Applications

PyQt/PySide

For a GUI application, you can also use the PyQt library. First, we need to install the library using the command below:

pip install PyQt6

Now, we can import the package and use the QtWidgets to print the bold text in the GUI window.

from PyQt6 import QtWidgets

app = QtWidgets.QApplication([])
label = QtWidgets.QLabel("<b>Bold Text</b>")
label.show()
app.exec()

Output of PyQt/PySide

Jupyter Notebooks

To print bold text in Jupyter notebooks, you can either use Markdown or HTML in notebook cells:

from IPython.display import Markdown, HTML

display(Markdown("**Bold Text**"))

display(HTML("<b>Bold Text</b>"))

Printing bold text in jupyter notebook

Logging

Most log viewers use ANSI codes. So, you can use libraries like colorlog to style logs.

First, you need to install the colorlog library:

pip install colorlog

Now, you can use the library:

import logging
import colorlog

# Create handler and formatter
handler = colorlog.StreamHandler()
formatter = colorlog.ColoredFormatter(
    "%(log_color)s%(bold)s%(message)s",  # Use %(bold)s for bold text
    log_colors={
        'INFO': 'green',
        'WARNING': 'yellow',
        'ERROR': 'red',
        'DEBUG': 'cyan',
        'CRITICAL': 'bold_red',
    },
    secondary_log_colors={},
    style='%'
)
handler.setFormatter(formatter)

# Set up logger
logger = colorlog.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.INFO)

# Log a message in bold
logger.info("This is bold info text")

Output of Logging

PDF Generation

For PDF-related generations, editing, or formatting, the most popular library is reportlab:

Install the library using the command below if not already installed:

pip install reportlab

Now, you can use its methods and attributes to print the bold text in the output PDFs.

from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

c = canvas.Canvas("bold_text.pdf", pagesize=letter)
c.setFont("Helvetica-Bold", 12)
c.drawString(100, 700, "Bold Text")
c.save()

Bold text in output PDF

That’s all!

Post Views: 234
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Print Without Newline in Python
How to Reverse a Range in Python

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend