How to Bold Text in Python. Python, as a versatile programming language, offers various methods for manipulating text output to include styles like bolding. This is particularly useful when designing console-based applications where you need to emphasize certain pieces of information.
Table of Contents
Using ANSI Escape Sequences for Bold Text
The most straightforward way to print bold text in a console is by using ANSI escape sequences. These are sequences of bytes used to control the appearance of text in video text terminals. Here is Python code that uses ANSI escape sequences to print bold text.
def print_bold(text):
BOLD = '\033[1m'
RESET = '\033[0m'
print(BOLD + text + RESET)
print_bold("Welcome to Python Tutorial")
print("Thanks for Visiting gangforcode.com")
Using Colorama for Printing Bold Text
‘Colorama’ is a popular Python library that makes ANSI escape character sequences work under Windows terminals as well as Unix-like terminals, Here is Python code using ‘Colorama’.
from colorama import init, Style
init(autoreset=True)
def print_bold(text):
print(Style.BRIGHT + text)
print_bold("Welcome to Python Tutorial")
print("Thanks for Visiting gangforcode.com")
The ‘autoreset=True’ argument automatically reset the style to normal after each print statement, making it easier to manage text styles without manually resetting them.
Happy Coding & Learning