How to Print Bold Text in Python
This guide explores how to print bold text to the console in Python. We'll cover using ANSI escape sequences for basic bolding (and other text styling), and then introduce popular libraries like termcolor, colorama, and simple-colors for more advanced color and style control.
Using ANSI Escape Sequences (Basic Bolding)
ANSI escape sequences are special character sequences that control text formatting in terminals. You can use them directly for basic bolding:
variable = 'this is bold'
print('this is NOT bold ' + '\033[1m' +
variable + '\033[0m' + ' this is NOT bold')
# Output: this is NOT bold [1mthis is bold[0m this is NOT bold
def bold_text(text): # Reusable function
bold_start = '\033[1m'
bold_end = '\033[0m'
return bold_start + text + bold_end
print('website: ' + bold_text('tutorialreference.com') + ' abc 123')
# Output: website: [1mtutorialreference.com[0m abc 123
\033[1m
: This sequence starts bold text.\033[0m
: This sequence resets all text formatting (including bolding). It's crucial to use this to avoid unintentionally bolding subsequent output.- You can also use f-strings for a cleaner version.
variable = 'this is bold'
print(f'this is NOT bold \033[1m{variable}\033[0m this is NOT bold')
# Output: this is NOT bold [1mthis is bold[0m this is NOT bold
Creating a Reusable Class
For more complex formatting, define a class to store ANSI escape sequences:
class Text:
BOLD_START = '\033[1m'
END = '\033[0m'
UNDERLINE = '\033[4m'
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
print(Text.BOLD_START + 'tutorialreference.com' + Text.END + ' abc 123')
# Output: [1mtutorialreference.com[0m abc 123
print(Text.BOLD_START + Text.YELLOW + 'tom' + Text.END + ' abc 123')
# Output: [1m[93mtom[0m abc 123
print(Text.BOLD_START + Text.UNDERLINE + Text.YELLOW +
'tom' + Text.END + ' abc 123')
# Output: [1m[4m[93mtom[0m abc 123
- This class defines constants for various text styles and colors.
- Use these constants to build your formatted strings.
ANSI escape sequences are supported by most modern terminals on Linux, macOS, and Windows (Windows 10 and later, with the virtual terminal enabled). However, older Windows versions or some specialized terminal emulators might not support them. In such cases, the escape sequences will appear as raw characters in the output. The libraries discussed below provide better cross-platform compatibility.
Using the termcolor
Library
The termcolor
library provides a simple, user-friendly interface for colored and styled text output. Install it:
pip install termcolor
Then use it like this:
from termcolor import colored
print('website: ' + colored('tutorialreference.com', attrs=['bold']) + ' abc 123')
# Output: website: tutorialreference.com abc 123 (tutorialreference.com is in bold)
print('website: ' + colored('tutorialreference.com', 'yellow', attrs=['bold']) + ' abc 123')
# Output: website: tutorialreference.com abc 123 (tutorialreference.com is bold and yellow)
colored(text, color=None, on_color=None, attrs=None)
: This function takes the text, an optional color, an optional background color (on_color
), and a list of attributes (like 'bold').- Available attributes include:
bold
,dark
,underline
,blink
,reverse
,concealed
.
Using the colorama
Library
colorama
is another popular library that provides cross-platform colored output, including support for older Windows consoles. It works by wrapping sys.stdout
and sys.stderr
and converting ANSI escape sequences into appropriate Windows API calls.
Install it:
pip install colorama
Use it like this:
from colorama import Style, Fore, Back, init
# Initialize colorama (important on Windows)
init()
variable = 'tutorialreference.com'
print('website: ' + Style.BRIGHT + variable + Style.RESET_ALL + ' abc 123')
# Output: website: tutorialreference.com abc 123 (tutorialreference.com is in bold)
print('website: ' + Style.BRIGHT + Fore.GREEN + variable + Style.RESET_ALL + ' abc 123')
# Output: website: tutorialreference.com abc 123 (tutorialreference.com bold and green)
Style.BRIGHT
: Makes the text bold (or bright, depending on the terminal).Fore.GREEN
: Sets the text color to green.Style.RESET_ALL
: Resets all formatting to defaults. This is essential to prevent styles from leaking to subsequent output.init()
: Theinit()
call is crucial on Windows. It enablescolorama
to work correctly by intercepting and translating ANSI codes. On other platforms, it's a no-op.
Using the simple-colors
Library
simple-colors
provides a more Pythonic and object-oriented approach to colored text:
pip install simple-colors
import simple_colors
print(
'website: ' +
simple_colors.green('tutorialreference.com', ['bold']) +
' abc 123')
# Output: website: tutorialreference.com abc 123 (tutorialreference.com bold and green)
print(
'website: ' +
simple_colors.blue('tutorialreference.com', ['bold', 'italic', 'underlined']) +
' abc 123')
# Output: website: tutorialreference.com abc 123 (tutorialreference.com is bold, blue, italic, underlined)
- The
simple_colors
module has functions for all basic colors. These functions take the text as the first argument and a list of styles as the second.