How to print Bold text in Python [5 simple Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. How to print Bold text in Python
  2. Extracting the ANSI escape sequences into a class
  3. Using the termcolor package to print bold text in Python
  4. Using the colorama package to print bold text in Python
  5. Using the simple-colors package to print bold text in Python

# How to print Bold text in Python

You can use an ANSI escape sequence to print bold text in Python.

ANSI escape sequences define functions that change display graphics.

The \033[1m character sequence is used to start bolding text and \033[0m is used to stop bolding text.

main.py
variable = 'this is bold' print('this is NOT bold ' + '\033[1m' + variable + '\033[0m' + ' this is NOT bold')

print bold text using ansi escape sequence

The code for this article is available on GitHub

You can also define a reusable function.

main.py
def bold_text(text): bold_start = '\033[1m' bold_end = '\033[0m' return bold_start + text + bold_end print('website: ' + bold_text('bobbyhadz.com') + ' abc 123')

bold text in python with reusable function

The example uses the addition (+) operator to concatenate the strings with the ANSI escape sequence.

The text between the \033[1m and \033[0m is printed in bold.

The remainder of the string is not printed in bold.

You can also use a formatted string literal to achieve the same result.

main.py
variable = 'this is bold' print(f'this is NOT bold \033[1m{variable}\033[0m this is NOT bold')

print bold text using formatted string literal

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

# Extracting the ANSI escape sequences into a class

You can also extract the ANSI escape sequences into a reusable class.

Here is an example of defining the escape sequences as class variables.

main.py
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 + 'bobbyhadz.com' + Text.END + ' abc 123')

extract ansi escape sequences into class

The code for this article is available on GitHub

You can access the class variables using dot notation as shown in the code sample.

The Text.END attribute ends the previously started ANSI escape sequence.

Here is an example of bolding and coloring the print output.

main.py
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 + Text.YELLOW + 'bobby' + Text.END + ' abc 123')

bolding and coloring print output

  1. We used the Text.BOLD_START attribute to start bolding text.
  2. Then we used the Text.YELLOW attribute to color the text yellow.
  3. The Text.END attribute ends the previously started ANSI escape sequences.

Here is an example that also uses the Text.UNDERLINE attribute.

main.py
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 + Text.UNDERLINE + Text.YELLOW + 'bobby' + Text.END + ' abc 123')

bold color underline text when printing

The code for this article is available on GitHub

# Using the termcolor package to print bold text in Python

You can also use the termcolor package to print bold text in Python.

Open your terminal and run the following command to install the package.

shell
pip install termcolor --upgrade # or with pip3 pip3 install termcolor --upgrade

Now import and use the termcolor package to bold text when printing.

main.py
from termcolor import colored print( 'website: ' + colored('bobbyhadz.com', attrs=['bold']) + ' abc 123')

bold text using termcolor

The code for this article is available on GitHub

The colored method from the termcolor package can be used to bold and color text when printing.

The attrs keyword argument is an array of the following attributes:

  • bold
  • dark
  • underline
  • blink
  • reverse
  • concealed

You can also pass a color as the second argument to the colored() method.

main.py
from termcolor import colored print('website: ' + colored('bobbyhadz.com', 'yellow', attrs=['bold']) + ' abc 123')

color bold text using termcolor

You can view all of the available colors and more examples of using the termcolor module on the package's Pypi page.

# Using the colorama package to print bold text in Python

You can also use the colorama package to print bold text in Python.

main.py
from colorama import Style variable = 'bobbyhadz.com' print('website: ' + Style.BRIGHT + variable + Style.RESET_ALL + ' abc 123')

print bold text using colorama

The code for this article is available on GitHub

The Style.BRIGHT attribute can be used to set the text to bright.

The Style.RESET_ALL attribute resets the foreground, background and brightness.

The colorama package can also be used to color text.

main.py
from colorama import Style, Fore variable = 'bobbyhadz.com' print('website: ' + Style.BRIGHT + Fore.GREEN + variable + Style.RESET_ALL + ' abc 123')

using colorama to bold and color text

The Fore class is used to set the foreground color.

You can check out more examples of using the colorama package in the module's Pypi page.

# Using the simple-colors package to print bold text in Python

You can also use the simple-colors package to print bold text in Python.

First, install the package by running the following command.

shell
pip install simple-colors --upgrade pip3 install simple-colors --upgrade

Now import and use the module as follows.

main.py
import simple_colors print( 'website: ' + simple_colors.green('bobbyhadz.com', ['bold']) + ' abc 123')

The simple-colors module supports the following color functions:

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan

Here is an example that bolds the text and prints it in red.

main.py
import simple_colors print( 'website: ' + simple_colors.red('bobbyhadz.com', ['bold']) + ' abc 123')

bold and color text using simple colors

The styles you can pass in the list argument are the following:

  • bold
  • bright
  • dim
  • italic
  • underlined
  • blink
  • reverse

You can also combine the styles.

main.py
import simple_colors print( 'website: ' + simple_colors.blue('bobbyhadz.com', ['bold', 'italic', 'underlined']) + ' abc 123')

print bold italic underlined text

The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.