Last updated: Apr 11, 2024
Reading time·4 min
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.
variable = 'this is bold' print('this is NOT bold ' + '\033[1m' + variable + '\033[0m' + ' this is NOT bold')
You can also define a reusable function.
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')
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.
variable = 'this is bold' print(f'this is NOT bold \033[1m{variable}\033[0m this is NOT bold')
f
.Make sure to wrap expressions in curly braces - {expression}
.
You can also extract the ANSI escape sequences into a reusable class.
Here is an example of defining the escape sequences as class variables.
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')
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.
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')
Text.BOLD_START
attribute to start bolding text.Text.YELLOW
attribute to color the text yellow.Text.END
attribute ends the previously started ANSI escape sequences.Here is an example that also uses the Text.UNDERLINE
attribute.
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')
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.
pip install termcolor --upgrade # or with pip3 pip3 install termcolor --upgrade
Now import and use the termcolor
package to bold text when printing.
from termcolor import colored print( 'website: ' + colored('bobbyhadz.com', attrs=['bold']) + ' abc 123')
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.
from termcolor import colored print('website: ' + colored('bobbyhadz.com', 'yellow', attrs=['bold']) + ' abc 123')
You can view all of the available colors and more examples of using the
termcolor
module on
the package's Pypi page.
You can also use the colorama package to print bold text in Python.
from colorama import Style variable = 'bobbyhadz.com' print('website: ' + Style.BRIGHT + variable + Style.RESET_ALL + ' abc 123')
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.
from colorama import Style, Fore variable = 'bobbyhadz.com' print('website: ' + Style.BRIGHT + Fore.GREEN + variable + Style.RESET_ALL + ' abc 123')
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.
You can also use the simple-colors package to print bold text in Python.
First, install the package by running the following command.
pip install simple-colors --upgrade pip3 install simple-colors --upgrade
Now import and use the module as follows.
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.
import simple_colors print( 'website: ' + simple_colors.red('bobbyhadz.com', ['bold']) + ' abc 123')
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.
import simple_colors print( 'website: ' + simple_colors.blue('bobbyhadz.com', ['bold', 'italic', 'underlined']) + ' abc 123')
You can learn more about the related topics by checking out the following tutorials: