Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Print Bold Text in Python?

#1
How to Print Bold Text in Python?

  • Summary: To print bold text in Python, you can use:
    • The simple_color package,
    • The ‘\033[1m’ ANSI escape-sequence,
    • The termcolor module,
    • The coloroma package,
    • The prompt_toolkit package.

You can change your text to bold, italic, and underlined in Python. Not only can you play around with the style of your code but also change its color with the help of specific packages and modules in Python. Interesting! Isn’t it? ?

There are different ways of doing this. By the end of this tutorial, you will be equipped with all the instruments to play around with the style of your code. So, without further delay, let the games begin!

? Method 1: Using The simple_color Package


This is one of the easiest methods to print bold text in Python. The simple_colors package includes many colors like blue, black, green, magenta, red, yellow, and cyan. You can also format your text in various styles like bold, dim, italic, bright, underlined, reverse and blink that are included in the package.

Since the simple_color package isn’t a part of Python’s standard library; you need to install it before utilizing it. To install the simple_color package copy the following code on your terminal:

pip install simple-colors

or,

python -m pip install simple-colors

After you have successfully installed the module, you can follow the syntax given in the example below to customize/style your code.

Example: The following example demonstrates how you can add color, format, and make the text bold or even underline it using the simple_colors module.

from simple_colors import * # normal and colored text
print('Normal:', blue('Welcome Finxter!'))
# print BOLD and colored text
print('BOLD: ', green('Welcome Finxter!', 'bold'))
# print BOLD and Underlined and colored text
print('BOLD and Underlined: ', red('Welcome Finxter!', ['bold', 'underlined']))

Output:

https://blog.finxter.com/wp-content/uplo...300x29.png 300w, https://blog.finxter.com/wp-content/uplo...768x75.png 768w, https://blog.finxter.com/wp-content/uplo...age-78.png 1279w" sizes="(max-width: 1024px) 100vw, 1024px" />

Hurrah! That look’s amazing. ?

? Method 2: Using The ANSI Escape Sequence \033[1m’


Some of the terminals support the capacity to pass in unique escape sequences to modify the tone, color, and appearance of the content being printed. These escape sequences are called ANSI escape sequences that got named after the ANSI standard that indicates their use.

Thus, you can utilize the built-in ANSI escape sequence to make the content or a specific text bold, underlined, italic, and even colored. To print the bold text in Python using the ANSI escape sequence, we use: ‘\033[1m’.

? Example 1: Escape-Sequence to print BOLD text for Windows Users

You have to call the os.system() module if you are using a Windows OS to make the ANSI escape sequence work properly.

import os
os.system ("color") c= { "head": "\033[95m", "red": "\033[91m", "ENDC": "\033[0m", "Bold": "\u001b[1m",
} print("\033[31;1;4mThis line gets printed in bold, underlined, and red color \033[0m")

Output:

https://blog.finxter.com/wp-content/uplo...300x12.png 300w, https://blog.finxter.com/wp-content/uplo...768x32.png 768w, https://blog.finxter.com/wp-content/uplo...age-79.png 1275w" sizes="(max-width: 1024px) 100vw, 1024px" />

#NOTE: The code ‘\033[0m’ is used to end the bold text format. If you forget to add the ANSI code sequence to enclose the specific line of code, then following statements will also be printed in bold format.

? Example 2: Escape-Sequence to print BOLD text for LINUX Users

print("Hello and welcome to my "'\033[1m' + ''Blog'' + '\033[0m')

? Method 3: Using termcolor Module


In Python, termcolor is a module utilized for the ANSII color formatting. The module comes with various properties for various terminals and certain text formatting properties. It also includes various text colors like blue, red, and green and text highlights like on-magenta, on-cyan, and on-white. Hence, we will use the bold property from the text attributes.

#Note: termcolor module isn’t a part of Python’s standard library. Thus, you need to install it before utilizing it. To install the termcolor module copy the following code on your terminal:

pip install termcolor

After installing the module, let’s visualize how you can use it to print the text in bold format.

Example:

from termcolor import colored text = colored('Hello and Welcome to FINXTER!', attrs=['bold']) # bold-text
print(text)
text2 = colored('This text will be printed in bold and blue color', 'blue', attrs=['bold'])
print(text2)

Output:

https://blog.finxter.com/wp-content/uplo...300x20.png 300w, https://blog.finxter.com/wp-content/uplo...768x51.png 768w, https://blog.finxter.com/wp-content/uplo...age-80.png 1278w" sizes="(max-width: 1024px) 100vw, 1024px" />

? Method 4: Using The Coloroma Package


  • ANSI escape character sequences have long been used to produce colored terminal text and cursor positioning on Unix and Macs. Colorama makes this work on Windows, too, by wrapping stdout, stripping ANSI sequences it finds (which would appear as gobbledygook in the output), and converting them into the appropriate win32 calls to modify the state of the terminal.
  • On other platforms, Colorama does nothing. In Windows, calling init() will shift the ANSI escape sequence of any content sent to stdout or stderr, and replace it with Win32 calls.
  • To quit utilizing Colorama before your program exits, call deinit(). It will restore the stdout and stderr to their original value.

Since coloroma is not a part of Python’s standard library, you have to install it using the following command in your terminal before you can use it.

pip install colorama

Let’s have a look at an example to understand how we can use the coloroma module to print bold text in Python.

Example 1:

from colorama import Fore, Style print(Style.BRIGHT + 'Bold text')
print(Fore.BLUE + 'Printing blue colored text')
print(Style.RESET_ALL)
print('This line has no effect of coloroma')

Output:

https://blog.finxter.com/wp-content/uplo...300x31.png 300w, https://blog.finxter.com/wp-content/uplo...768x79.png 768w, https://blog.finxter.com/wp-content/uplo...age-81.png 1276w" sizes="(max-width: 1024px) 100vw, 1024px" />

Example 2: Using coloroma package along with the termcolor module.

from colorama import init
from termcolor import colored init()
print(colored('Hello and Welcome to Finxter!', 'blue', attrs=['bold']))

Output:

https://blog.finxter.com/wp-content/uplo...300x28.png 300w" sizes="(max-width: 718px) 100vw, 718px" />

? Method 5: Create an Html object 


  • Prompt_toolkit includes a print_formatted_text() function that is compatible (as much as possible) with the built-in print function. It also supports colors and formatting.
  • HTML can be utilized to demonstrate that a string contains HTML based formatting. Thus, the HTML object recognizes the essential tags for bold, italic and underline: <b>, <i> and <u>.

Example 1:

from prompt_toolkit import print_formatted_text, HTML print_formatted_text(HTML('<b>This text is bold</b>'))
print_formatted_text(HTML('<i>This text is italic</i>'))
print_formatted_text(HTML('<u>This text is underlined</u>'))

Output:

https://blog.finxter.com/wp-content/uplo...300x31.png 300w" sizes="(max-width: 584px) 100vw, 584px" />

Example 2:

As all the HTML tags are mapped to classes from a style sheet, you can also appoint a style for a custom tag.

from prompt_toolkit import print_formatted_text, HTML
from prompt_toolkit.styles import Style sty = Style.from_dict({ 'y': '#44ff00 bold',
}) print_formatted_text (HTML('<y> Hello and welcome to <u>Finxter!</u> </y>'), style=sty)

Output:

https://blog.finxter.com/wp-content/uplo...300x24.png 300w" sizes="(max-width: 561px) 100vw, 561px" />

Conclusion


We have finally conquered the art of printing bold texts in Python. Not only did we learn how to print bold texts, but we also learned how to style the code using colors and other formatting styles like underline and italics. I hope this article helped you.

Please stay tuned and subscribe for more interesting articles!

Thank you, Rashi Agarwal, for helping me with this article.

The Complete Guide to PyCharm

  • Do you want to master the most popular Python IDE fast?
  • This course will take you from beginner to expert in PyCharm in ~90 minutes.
  • For any software developer, it is crucial to master the IDE well, to write, test and debug high-quality code with little effort.

Join the PyCharm Masterclass now, and master PyCharm by tomorrow!

The post How to Print Bold Text in Python? first appeared on Finxter.



https://www.sickgaming.net/blog/2021/03/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Extract Text from Images in Python using OpenCV and EasyOCR xSicKxBot 0 26 03-06-2021, 01:40 PM
Last Post: xSicKxBot
  [Tut] How To Extract All Emojis From Text in Python? xSicKxBot 0 30 02-28-2021, 01:58 PM
Last Post: xSicKxBot
  [Tut] How to Print an Integer with Commas as Thousands Separators in Python? xSicKxBot 0 46 02-08-2021, 02:31 PM
Last Post: xSicKxBot
  [Tut] Python help() xSicKxBot 0 65 01-22-2021, 01:02 AM
Last Post: xSicKxBot
  [Tut] Python complex() — A Useless Python Feature? xSicKxBot 0 73 12-19-2020, 02:05 AM
Last Post: xSicKxBot
  [Tut] How to Catch and Print Exception Messages in Python xSicKxBot 0 66 11-23-2020, 11:45 PM
Last Post: xSicKxBot
  [Tut] Python next() xSicKxBot 0 69 11-22-2020, 08:17 AM
Last Post: xSicKxBot
  [Tut] How to Generate Text Automatically With Python? A Guide to the DeepAI API xSicKxBot 0 102 11-10-2020, 06:23 AM
Last Post: xSicKxBot
  [Tut] Hello World! A Python One-Liner to Get Started with Python Quickly xSicKxBot 0 118 09-07-2020, 06:10 PM
Last Post: xSicKxBot
  [Tut] Pretty Print JSON [Python One-Liner] xSicKxBot 0 137 08-26-2020, 01:20 PM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
[Tut] How to Display an Image as Graysca...
Last Post: xSicKxBot
Today 09:01 PM
» Replies: 0
» Views: 0
(Indie Deal) SNK's Metal Slug, King of F...
Last Post: xSicKxBot
Today 09:01 PM
» Replies: 0
» Views: 0
Xbox Wire - Monster Train: The Last Divi...
Last Post: xSicKxBot
Today 09:00 PM
» Replies: 0
» Views: 0
News - Wargaming opens Lithuania studio ...
Last Post: xSicKxBot
Today 09:00 PM
» Replies: 0
» Views: 0
News - Get a job: Join Volition as a Sen...
Last Post: xSicKxBot
Today 09:00 PM
» Replies: 0
» Views: 0
PluralSight Courses All Free During Apri...
Last Post: xSicKxBot
Today 05:10 PM
» Replies: 0
» Views: 6
AppleInsider - Flash deal: 50% off H&R B...
Last Post: xSicKxBot
Today 05:10 PM
» Replies: 0
» Views: 6
News - See More Of New Zombie Game The D...
Last Post: xSicKxBot
Today 05:09 PM
» Replies: 0
» Views: 5
ISO: Staff Members for Website, Discord,...
Last Post: Ronalddiz
Today 04:20 PM
» Replies: 4
» Views: 8453
Official Sick Gaming Social Links: Twitt...
Last Post: Marcusjut
Today 09:56 AM
» Replies: 5
» Views: 9835

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020