Question

[Solved] How do I print bold text in Python?

How do I print bold text in Python?

For example:

print "hello"

What should I do so that the text “hello” is displayed in bold?

Enquirer: Jia-Luo

||

Solution #1:

class color:
   PURPLE = '33[95m'
   CYAN = '33[96m'
   DARKCYAN = '33[36m'
   BLUE = '33[94m'
   GREEN = '33[92m'
   YELLOW = '33[93m'
   RED = '33[91m'
   BOLD = '33[1m'
   UNDERLINE = '33[4m'
   END = '33[0m'

print(color.BOLD + 'Hello World !' + color.END)
Respondent: Bouba

Solution #2:

Use this:

print '33[1m' + 'Hello'

And to change back to normal:

print '33[0m'

This page is a good reference for printing in colors and font-weights. Go to the section that says ‘Set graphics mode:’

And note this won’t work on all operating systems but you don’t need any modules.

Respondent: Addison

Solution #3:

You can use termcolor for this:

 sudo pip install termcolor

To print a colored bold:

 from termcolor import colored
 print(colored('Hello', 'green', attrs=['bold']))

For more information, see termcolor on PyPi.

simple-colors is another package with similar syntax:

 from simple_colors import *
 print(green('Hello', ['bold'])

The equivalent in colorama may be Style.BRIGHT.

Respondent: Zuko

Solution #4:

In straight-up computer programming, there is no such thing as “printing bold text”. Let’s back up a bit and understand that your text is a string of bytes and bytes are just bundles of bits. To the computer, here’s your “hello” text, in binary.

0110100001100101011011000110110001101111

Each one or zero is a bit. Every eight bits is a byte. Every byte is, in a string like that in Python 2.x, one letter/number/punctuation item (called a character). So for example:

01101000 01100101 01101100 01101100 01101111
h        e        l        l        o

The computer translates those bits into letters, but in a traditional string (called an ASCII string), there is nothing to indicate bold text. In a Unicode string, which works a little differently, the computer can support international language characters, like Chinese ones, but again, there’s nothing to say that some text is bold and some text is not. There’s also no explicit font, text size, etc.

In the case of printing HTML, you’re still outputting a string. But the computer program reading that string (a web browser) is programmed to interpret text like this is <b>bold</b> as “this is bold” when it converts your string of letters into pixels on the screen. If all text were WYSIWYG, the need for HTML itself would be mitigated — you would just select text in your editor and bold it instead of typing out the HTML.

Other programs use different systems — a lot of answers explained a completely different system for printing bold text on terminals. I’m glad you found out how to do what you want to do, but at some point, you’ll want to understand how strings and memory work.

Respondent: Ken Kinder

Solution #5:

This depends if you’re using linux/unix:

>>> start = "33[1m"
>>> end = "33[0;0m"
>>> print "The" + start + "text" + end + " is bold."
The text is bold.

The word text should be bold.

Respondent: aayoubi

Solution #6:

Check out colorama. It doesn’t necessarily help with bolding… but you can do colorized output on both Windows and Linux, and control the brightness:

from colorama import *
init(autoreset=True)
print Fore.RED + 'some red text'
print Style.BRIGHT + Fore.RED + 'some bright red text'
Respondent: John Szakmeister

Solution #7:

There is a very useful module for formatting text (bold, underline, colors..) in Python. It uses curses lib but it’s very straight-forward to use.

An example:

from terminal import render
print render('%(BG_YELLOW)s%(RED)s%(BOLD)sHey this is a test%(NORMAL)s')
print render('%(BG_GREEN)s%(RED)s%(UNDERLINE)sAnother test%(NORMAL)s')

UPDATED:

I wrote a simple module named colors.py to make this a little more pythonic:

import colors

with colors.pretty_output(colors.BOLD, colors.FG_RED) as out:
    out.write("This is a bold red text")

with colors.pretty_output(colors.BG_GREEN) as out:
    out.write("This output have a green background but you " + 
               colors.BOLD + colors.FG_RED + "can" + colors.END + " mix styles")
Respondent: Diego Navarro

Solution #8:

print '33[1m  Your Name  33[0m'

33[1m is the unicode for bold in the terminal
33[0m is the unicode for end the edited text and back default text formate!!!!!

if you do not use 33[0m than all upcoming text of the terminal will become bold!!!!!!!!!

Respondent: Bhavik Sakhiya

Solution #9:

Install the termcolor module

sudo pip install termcolor

and then try this for colored text

from termcolor import colored
print colored('Hello', 'green')

or this for bold text:

from termcolor import colored
print colored('Hello', attrs=['bold'])

In Python 3 you can alternatively use cprint as a drop-in replacement for the built-in print, with the optional second parameter for colors or the attrs parameter for bold (and other attributes such as underline) in addition to the normal named print arguments such as file or end.

import sys
from termcolor import cprint
cprint('Hello', 'green', attrs=['bold'], file=sys.stderr)

Full disclosure, this answer is heavily based on Olu Smith’s answer
and was intended as an edit, which would have reduced the noise on this page
considerably but because of some reviewers’ misguided concept of
what an edit is supposed to be, I am now forced to make this a separate answer.

Respondent: Christian

Solution #10:

Some terminals allow to print colored text. Some colors look like if they are “bold”. Try:

print ('33[1;37mciao!')

The sequence ‘33[1;37m’ makes some terminals to start printing in “bright white” that may look a bit like bolded white. ‘33[0;0m’ will turn it off.

Respondent: Maciek

Solution #11:

Assuming that you really mean “print” on a real printing terminal:

>>> text = 'foo barrnooftrabrn'
>>> ''.join(s if i & 1 else (s + 'b' * len(s)) * 2 + s
...         for i, s in enumerate(re.split(r'(s+)', text)))
'foox08x08x08foox08x08x08foo barx08x08x08barx08x08x08barrnoofx08
x08x08oofx08x08x08ooftrabx08x08x08rabx08x08x08rabrn'

Just send that to your stdout.

Respondent: John Machin

Solution #12:

Simple Boldness – Two Line Code

In python 3 you could use colorama – simple_colors:
(Simple Colours page: https://pypi.org/project/simple-colors/ – go to the heading ‘Usage’.) Before you do what is below, make sure you pip install simple_colours.

from simple_colors import *
print(green('hello', 'bold'))

enter image description here

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy