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)
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)
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
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']))
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()
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()
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>"))
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")
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()
That’s all!