- How to print bold text in Python?
- Solution 3 — Python
- Solution 4 — Python
- Solution 5 — Python
- Solution 6 — Python
- UPDATED:
- Solution 7 — Python
- Solution 8 — Python
- Solution 9 — Python
- Solution 10 — Python
- Solution 11 — Python
- Solution 12 — Python
- Simple Boldness — Two Line Code
- Solution 13 — Python
- Solution 14 — Python
- Print Bold Text in Python
- Print Bold Text in Python Using the ANSI Escape Sequence Method
- Print Bold Text in Python Using the color Class
- Print Bold Text in Python Using the termcolor Method
- Print Bold Text in Python Using the colorama Package
- Print Bold Text in Python Using the simple_color Package
- Related Article — Python Print
- How to Print Bold Text in Python
- Using the termcolor
- Using the color Class
- Using the Colorama package
- Using Prompt_toolkit package
How to print bold text in Python?
And note this won’t work on all operating systems but you don’t need any modules.
Solution 3 — Python
You can use termcolor for this:
sudo pip install termcolor
from termcolor import colored print(colored('Hello', 'green', attrs=['bold']))
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 — Python
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 bold 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 — Python
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.
Solution 6 — Python
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.
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")
Solution 7 — Python
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'
Solution 8 — Python
print '\033[1m Your Name \033[0m'
\033[1m is the escape code for bold in the terminal. \033[0m is the escape code for end the edited text and back default text format.
If you do not use \033[0m then all upcoming text of the terminal will become bold.
Solution 9 — Python
Install the termcolor module
sudo pip install termcolor
and then try this for colored text
from termcolor import colored print colored('Hello', 'green')
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.
Solution 10 — Python
Some terminals allow to print colored text. Some colors look like if they are «bold». Try:
The sequence ‘\033[1;37m’ makes some terminals to start printing in «bright white» that may look a bit like bolded white. ‘\033[0;0m’ will turn it off.
Solution 11 — Python
Assuming that you really mean «print» on a real printing terminal:
>>> text = 'foo bar\r\noof\trab\r\n' >>> ''.join(s if i & 1 else (s + '\b' * len(s)) * 2 + s . for i, s in enumerate(re.split(r'(\s+)', text))) 'foo\x08\x08\x08foo\x08\x08\x08foo bar\x08\x08\x08bar\x08\x08\x08bar\r\noof\x08\ x08\x08oof\x08\x08\x08oof\trab\x08\x08\x08rab\x08\x08\x08rab\r\n'
Just send that to your stdout .
Solution 12 — Python
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'))
Solution 13 — Python
Printing in bold made easy. Install quo using pip
from quo import echo echo(f"Hello World!!", bold=True)
Solution 14 — Python
A simple approach relies on Unicode Mathematical Alphanumeric Symbols.
Code
def bold( text, trans=str.maketrans( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "𝗔𝗕𝗖𝗗𝗘𝗙𝗚𝗛𝗜𝗝𝗞𝗟𝗠𝗡𝗢𝗣𝗤𝗥𝗦𝗧𝗨𝗩𝗪𝗫𝗬𝗭𝗮𝗯𝗰𝗱𝗲𝗳𝗴𝗵𝗶𝗷𝗸𝗹𝗺𝗻𝗼𝗽𝗾𝗿𝘀𝘁𝘂𝘃𝘄𝘅𝘆𝘇𝟬𝟭𝟮𝟯𝟰𝟱𝟲𝟳𝟴𝟵", ), ): return text.translate(trans)
Example
Discussion
Several pros and cons I can think of. Feel free to add yours in the comments.
- As short as readable.
- No external library.
- Portable: can be used for instance to highlight sections in an ipywidgets Dropdown .
- Extensible to italics, etc. with the appropriate translation tables.
- Language agnostic: the same technic can be implemented in any programming language.
- Requires Unicode support and a font where all the required glyphs are defined. This should be ok on any reasonably modern system, though.
- No copy-paste : produces a faux-text. Note that ‘𝘄𝗼𝗿𝗹𝗱’.isalpha() is still True , though.
- No diacritics.
Implementation notes
- In the code above, the translation table is given as an optional argument, meaning that it is evaluated only once, and conveniently encapsulated in the function which makes use it. If you prefer a more standard style, define a global BOLD_TRANS constant, or use a closure or a lightweight class.
Print Bold Text in Python
- Print Bold Text in Python Using the ANSI Escape Sequence Method
- Print Bold Text in Python Using the color Class
- Print Bold Text in Python Using the termcolor Method
- Print Bold Text in Python Using the colorama Package
- Print Bold Text in Python Using the simple_color Package
This article will discuss some methods to print bold text in Python.
Print Bold Text in Python Using the ANSI Escape Sequence Method
We can use built-in ANSI escape sequences for making text bold, italic or colored, etc. By using the special ANSI escape sequences, the text can be printed in different formats. The ANSI escape sequence to print bold text is: ‘\033[1m’ . To print the bold text, we use the following statement.
print("The bold text is",'\033[1m' + 'Python' + '\033[0m')
Here, ‘\033[0m’ ends the bold formatting. If it is not added, the next print statement will keep print the bold text.
Print Bold Text in Python Using the color Class
This method creates a color class. ANSI escape sequence of all the colors is listed in the class. To print the color of our own choice, we can select any of the colors.
The complete example code is given below.
class bold_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("The output is:" + color.BOLD + 'Python Programming !' + color.BLUE)
Print Bold Text in Python Using the termcolor Method
The termcolor is a package for ANSI color formatting for output in the terminal with different properties for different terminals and certain text properties. We will use bold text attributes in this function. The colored() function gives the text the specific color and makes it bold.
The complete example code is given below.
from termcolor import colored print(colored('python', 'green', attrs=['bold']))
Print Bold Text in Python Using the colorama Package
It is a cross-platform for colored terminal text. It makes ANSI works under MS Windows for escape character sequences. To use this package, you must install it in your terminal by the following command. If you have not installed it, then the code will not work properly.
pip install colorama conda install -c anaconda colorama
The complete example code is given below:
from colorama import init from termcolor import colored init() print(colored('Python Programming !', 'green', 'on_red'))
We use the colorama module with termcolor , to print colored text on the Windows terminal. Calling init() on Windows would filter ANSI escape sequences out of every other text sent to stdout or stderr , replacing them with Win32 equivalent calls. The colored() function will color the specified string in the green color.
Print Bold Text in Python Using the simple_color Package
We must install this package by the following command.
pip install simple_colours
It is the simplest method to print bold text in Python.
The complete example code is given below:
from simple_colors import * print(green('Python', 'bold'))
Related Article — Python Print
How to Print Bold Text in Python
To print bold text in Python, you can use the built-in “ANSI escape sequences” to make text bold, italic, or colored. The text can be printed using the particular ANSI escape sequences in different formats.
The ANSI escape sequence to print bold text in Python is: ‘\033[1m’.
print("This is bold text looks like:",'\033[1m' + 'Python' + '\033[0m')
You can see from the output that Python is bold. Although, my console is zsh. So it displays white color. But you can think of it as bold text.
Using the termcolor
The termcolor is a package for ANSI color formatting for output in the terminal with different properties for different terminals and specific text properties. We will use bold text attributes in this function. The colored() function gives the text a specific color and makes it bold.
We first install the termcolor module.
Next, we use pip to install packages in Python.
python3 -m pip install termcolor
Now, let’s write the colored text.
from termcolor import colored print(colored('python', 'red', attrs=['bold']))
You can count the above text as red-colored text in the output.
Using the color Class
In this approach, we will create a color class. Then, the ANSI escape sequence of all the colors is listed in the class. Then, to print the color of our choice, we can select any colors.
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("The output is:" + color.BLUE + 'Python 3!')
Using the Colorama package
To work with the Colorama package, you need to install the package.
python3 -m pip install colorama
It is a cross-platform for colored terminal text. In addition, it makes ANSI works under Microsoft Windows for escape character sequences.
from colorama import init from termcolor import colored init() print(colored('Python 3 !', 'green', 'on_red'))
We used a Colorama module with termcolor to print colored text on the Windows terminal.
Calling init() on Windows would filter ANSI escape sequences out of every other text sent to stdout or stderr, replacing them with Win32 equivalent calls. In addition, the colored() function will color the specified string green.
Using Prompt_toolkit package
Prompt_toolkit includes a print_formatted_text() function that is compatible (as much as possible) with the built-in function. It also supports colors and formatting.
from prompt_toolkit import print_formatted_text, HTML print_formatted_text(HTML('The text is bold')) print_formatted_text(HTML('The text is italic')) print_formatted_text(HTML('The text is underlined'))