How to print bold text in Python?
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.
Related videos on Youtube
Jia-Luo
Updated on July 08, 2022Comments
-
Jia-Luo almost 2 years
E.g:
print "hello"
What should I do to make the text "hello" bold?
-
Joe over 12 yearsduplicate 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 over 12 yearsWhich terminal are you using? Are you on Unix or Windows?
-
Jia-Luo over 12 yearsi'm using safari. Just found out i can use HTML tags in python.
-
-
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 about 11 yearsThe above link used to contain the
terminal
module, but they have redirect the page. Here is the code cached by google. -
Diego Navarro about 11 yearsAnyways, I have made my own python module to solve this, check it out @minerals ;-)
-
John Y over 9 yearsAs 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 over 7 yearsI 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 over 7 yearsI'm gonna use this as dict, not a class. Thanks!
-
Trevor about 7 yearsThe 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 almost 7 yearsIt is printing the following :
[1mHello World ![0m
-
Harsha Biyani about 6 yearsplease add some explanation
-
Christian over 4 yearsIf I had to guess someone downvoted this because it didn't answer the actual question how to print something in bold.
-
yzerman over 4 yearsThanks! We just need to first install simple_colors:
pip install simple_colors
-
Elouan Keryell-Even over 4 yearsWorking on my setup: ubuntu 19.10, python 3.7
-
alper about 4 yearsCan I combine bold with color as well? @Boubakr
-
edison23 about 4 yearsYes, 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 about 4 yearsYour right, there's been an update. Sorry about that guys.
-
Denis Savenko over 3 yearsThank you for awesome simple solution. I think for this we can try use split
settings.ini
file with constants and pythonconfigparser
for download it and share between many projects. -
CoolCoder about 3 yearsTo all Windows users, note: This is not working on Windows (Windows 10)
-
Mohammed Deifallah over 2 yearsThis should be the accepted answer :)
-
mouwsy over 2 yearsFor me it works on Windows 10 with
print('\033[1m' + 'Hello')
. -
Earl Mascetti over 2 yearsThank you. I want to report you that the link doesn't work.
-
Addison over 2 years@EarlMascetti Thanks for letting me know! I just updated the link to use the wayback machine