Here are five ways to print bold text in Python:
- Using ANSI escape
- Using termcolor module
- Using simple_color package
- Using Colorama package
- 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
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
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
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
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
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
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
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
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
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
That’s it.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.