Skip to content

How to Print Bold Text in Python

Learn how to print bold text in Python using ANSI escape sequences, termcolor, colorama, and simple-colors for cross-platform console formatting.

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

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')
  • \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')

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

print(Text.BOLD_START + Text.YELLOW + 'tom' + Text.END + ' abc 123')

print(Text.BOLD_START + Text.UNDERLINE + Text.YELLOW +
        'tom' + Text.END + ' abc 123')
  • This class defines constants for various text styles and colors.
  • Use these constants to build your formatted strings.

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')
print('website: ' + colored('tutorialreference.com', 'yellow', attrs=['bold']) + ' abc 123')
  • 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

init()

variable = 'tutorialreference.com'
print('website: ' + Style.BRIGHT + variable + Style.RESET_ALL + ' abc 123')

print('website: ' + Style.BRIGHT + Fore.GREEN + variable + Style.RESET_ALL + ' abc 123')
  • 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(): The init() call is crucial on Windows. It enables colorama 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')

print(
'website: ' +
    simple_colors.blue('tutorialreference.com', ['bold', 'italic', 'underlined']) +
    ' abc 123')
  • 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.
Navigation