How to Print Bold Text in Python?

  • Summary: To print bold text in Python, you can use:
    • The simple_color package,
    • The '\033[1m' ANSI escape-sequence,
    • The termcolor module,
    • The coloroma package,
    • The prompt_toolkit package.

A simple, no-library way to print bolded text in Python is to enclose a given string s in the special escape sequence like so: print("\033[1m" + s + "\033[0m").

We’ll discuss this most Pythonic solution in Method 2 in this article.


You can change your text to bold, italic, and underlined in Python. Not only can you play around with the style of your code but also change its color with the help of specific packages and modules in Python.

Interesting! Isn’t it?

There are different ways of doing this. By the end of this tutorial, you will be equipped with all the instruments to play around with the style of your code.

So, without further delay, let the games begin!

πŸ“ΉVideo Walkthrough

Method 1: Using The simple_color Package

This is one of the easiest methods to print bold text in Python. The simple_colors package includes many colors like blue, black, green, magenta, red, yellow, and cyan.

You can also format your text in various styles like bold, dim, italic, bright, underlined, reverse and blink that are included in the package.

Since the simple_color package isn’t a part of Python’s standard library; you need to install it before utilizing it. To install the simple_color package, copy the following code on your terminal:

pip install simple-colors

or,

python -m pip install simple-colors

After you have successfully installed the module, you can follow the syntax given in the example below to customize/style your code.

Example: The following example demonstrates how you can add color, format, and make the text bold or even underline it using the simple_colors module.

from simple_colors import *

# normal and colored text
print('Normal:', blue('Welcome Finxter!'))
# print BOLD and colored text
print('BOLD: ', green('Welcome Finxter!', 'bold'))
# print BOLD and Underlined and colored text
print('BOLD and Underlined: ', red('Welcome Finxter!', ['bold', 'underlined']))

Output:

Hurrah! That look’s amazing.

Method 2: Using The ANSI Escape Sequence \033[1m’

The Most Pythonic way to print bolded text in Python is to enclose a given string s in the special escape sequence like so: print("\033[1m" + s + "\033[0m").

Here’s a minimal example:

s = "abc"
bold_s = "\033[1m" + s + "\033[0m"
print(bold_s)
print(s)

You can try this yourself in our interactive Jupyter notebook:

🧩 Interactive: Try it yourself in Google Colab

Let’s dive into some further explanations to see why this works next.

Some terminals support the capacity to pass in unique escape sequences to modify the tone, color, and appearance of the content being printed.

These escape sequences are called ANSI escape sequences that got named after the ANSI standard that indicates their use. 

Thus, you can utilize the built-in ANSI escape sequence to make the content or a specific text bold, underlined, italic, and even colored. To print the bold text in Python using the ANSI escape sequence, we use: '\033[1m'.

Example 1: Escape-Sequence to print BOLD text for Windows Users

You have to call the os.system() module if you are using a Windows OS to make the ANSI escape sequence work properly.

import os
os.system ("color")

c= {
    "head": "\033[95m",
    "red": "\033[91m",
    "ENDC": "\033[0m",
    "Bold": "\u001b[1m",
}

print("\033[31;1;4mThis line gets printed in bold, underlined, and red color \033[0m")

Output:

πŸ’‘ NOTE: The code '\033[0m' is used to end the bold text format. If you forget to add the ANSI code sequence to enclose the specific line of code, the following statements will also be printed in bold format.

Example 2: Escape-Sequence to print BOLD text for LINUX Users

print("Hello and welcome to my "'\033[1m' + ''Blog'' + '\033[0m')

Method 3: Using termcolor Module

In Python, termcolor is a module utilized for the ANSII color formatting.

The module comes with various properties for various terminals and certain text formatting properties. It also includes various text colors like blue, red, and green and text highlights like on-magenta, on-cyan, and on-white.

Hence, we will use the bold property from the text attributes.

πŸ’‘ Note: termcolor module isn’t a part of Python’s standard library. Thus, you need to install it before utilizing it. To install the termcolor module copy the following code on your terminal:

pip install termcolor

After installing the module, let’s visualize how you can use it to print the text in bold format.

Example:

from termcolor import colored

text = colored('Hello and Welcome to FINXTER!', attrs=['bold'])  # bold-text
print(text)
text2 = colored('This text will be printed in bold and blue color', 'blue', attrs=['bold'])
print(text2)

Output:

Method 4: Using The Coloroma Package

ANSI escape character sequences have long been used to produce colored terminal text and cursor positioning on Unix and Macs.

Colorama makes this work on Windows. By wrapping stdout, stripping ANSI sequences it finds (which would appear as gobbledygook in the output), and converting them into the appropriate win32 calls to modify the state of the terminal.

On other platforms, Colorama does nothing. In Windows, calling init() will shift the ANSI escape sequence of any content sent to stdout or stderr, and replace it with Win32 calls.

To quit utilizing Colorama before your program exits, call deinit(). It will restore the stdout and stderr to their original value.

Source: https://pypi.org/project/colorama/

Since coloroma is not a part of Python’s standard library, you have to install it using the following command in your terminal before you can use it.

pip install colorama

Let’s have a look at an example to understand how we can use the Colorama module to print bold text in Python.

Example 1:

from colorama import Fore, Style

print(Style.BRIGHT + 'Bold text')
print(Fore.BLUE + 'Printing blue colored text')
print(Style.RESET_ALL)
print('This line has no effect of coloroma')

Output:

Example 2: Using coloroma package along with the termcolor module.

from colorama import init
from termcolor import colored

init()
print(colored('Hello and Welcome to Finxter!', 'blue', attrs=['bold']))

Output:

Method 5: Create an HTML object 

Prompt_toolkit includes a print_formatted_text() function that is compatible (as much as possible) with the built-in print() function. It also supports colors and formatting.

HTML can be utilized to demonstrate that a string contains HTML based formatting. Thus, the HTML object recognizes the essential tags for bold, italic and underline: <b>, <i> and <u>.

Example 1:

from prompt_toolkit import print_formatted_text, HTML

print_formatted_text(HTML('<b>This text is bold</b>'))
print_formatted_text(HTML('<i>This text is italic</i>'))
print_formatted_text(HTML('<u>This text is underlined</u>'))

Output:

Example 2:

As all the HTML tags are mapped to classes from a style sheet, you can also appoint a style for a custom tag.

from prompt_toolkit import print_formatted_text, HTML
from prompt_toolkit.styles import Style

sty = Style.from_dict({
       'y': '#44ff00 bold',
})

print_formatted_text (HTML('<y> Hello and welcome to <u>Finxter!</u> </y>'), style=sty)

Output:

Conclusion

We have finally conquered the art of printing bold texts in Python. Not only did we learn how to print bold texts, but we also learned how to style the code using colors and other formatting styles like underlining and italics. I hope this article has helped you.

Related Tutorials:

Please stay tuned and subscribe for more interesting articles!

Thank you, Rashi Agarwal, for helping me with this article.