How to print bold text in Python?

491,720

Solution 1

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

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

Solution 2

Use this:

print '\033[1m' + 'Hello'

And to change back to normal:

print '\033[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.

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.

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.

Solution 5

This depends if you're using linux/unix:

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

The word text should be bold.

Share:
491,720

Related videos on Youtube

Jia-Luo
Author by

Jia-Luo

Updated on July 08, 2022

Comments

  • Jia-Luo
    Jia-Luo almost 2 years

    E.g:

    print "hello"
    

    What should I do to make the text "hello" bold?

    • Joe
      Joe over 12 years
      duplicate of color text in terminal aplications in unix . Lots of links in the answers. That answer is in C, but easily translated to Python.
    • Sjoerd
      Sjoerd over 12 years
      Which terminal are you using? Are you on Unix or Windows?
    • Jia-Luo
      Jia-Luo over 12 years
      i'm using safari. Just found out i can use HTML tags in python.
  • minerals
    minerals about 11 years
    ImportError: No module named terminal ImportError: No module named render Actually this is the only site I could find about "terminal" module. Please elaborate.
  • Diego Navarro
    Diego Navarro about 11 years
    The above link used to contain the terminal module, but they have redirect the page. Here is the code cached by google.
  • Diego Navarro
    Diego Navarro about 11 years
    Anyways, I have made my own python module to solve this, check it out @minerals ;-)
  • John Y
    John Y over 9 years
    As unsatisfying as this answer might seem, it's probably the one which most properly answers the question as asked. It's not a Python question at all, but rather a whatever-is-doing-the-rendering question.
  • GreenHawk1220
    GreenHawk1220 over 7 years
    I like the way you didn't just did bold, but created a whole class for them to reference and to help all users viewing. Thank you.
  • dvlden
    dvlden over 7 years
    I'm gonna use this as dict, not a class. Thanks!
  • Trevor
    Trevor about 7 years
    The thing is, everybody interested in the answer to this question understands binary and how it's used to represent strings. We're interested in how to use python to make text look bold, which is answered above. This answer is way over pedantic.
  • Admin
    Admin almost 7 years
    It is printing the following : [1mHello World ![0m
  • Harsha Biyani
    Harsha Biyani about 6 years
    please add some explanation
  • Christian
    Christian over 4 years
    If I had to guess someone downvoted this because it didn't answer the actual question how to print something in bold.
  • yzerman
    yzerman over 4 years
    Thanks! We just need to first install simple_colors: pip install simple_colors
  • Elouan Keryell-Even
    Elouan Keryell-Even over 4 years
    Working on my setup: ubuntu 19.10, python 3.7
  • alper
    alper about 4 years
    Can I combine bold with color as well? @Boubakr
  • edison23
    edison23 about 4 years
    Yes, just concatenate the two vars: print(color.RED + color.BOLD + "Bold red string" + color.END) . (BTW, works with Python 3.8.2 (at least on Ubuntu 20.04).)
  • Zuko
    Zuko about 4 years
    Your right, there's been an update. Sorry about that guys.
  • Denis Savenko
    Denis Savenko over 3 years
    Thank you for awesome simple solution. I think for this we can try use split settings.ini file with constants and python configparser for download it and share between many projects.
  • CoolCoder
    CoolCoder about 3 years
    To all Windows users, note: This is not working on Windows (Windows 10)
  • Mohammed Deifallah
    Mohammed Deifallah over 2 years
    This should be the accepted answer :)
  • mouwsy
    mouwsy over 2 years
    For me it works on Windows 10 with print('\033[1m' + 'Hello').
  • Earl Mascetti
    Earl Mascetti over 2 years
    Thank you. I want to report you that the link doesn't work.
  • Addison
    Addison over 2 years
    @EarlMascetti Thanks for letting me know! I just updated the link to use the wayback machine