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.
Related Tutorials:
- How to Print Bold Text in Python?
- How to Print Italic Text in Python?
- How to Print Underlined Text in Python?
- How to Print Colored Text in Python?
The following video also mentions how to print underlined and colored text—feel free to watch it as you go over the tutorial:
So, without further delay, let the games begin!
βοΈ Problem Formulation: Given a string. How to print the string as underlined text in Python?
Method 1: Enclosing String in ANSI Escape Sequence ‘\x1B[3m’ and ‘\x1B[0m’
The most straightforward way to print underlined text in Python is to enclose a given string text
in the special ANSI escape sequence like so: print("\x1B[4m" + text + "\x1B[0m")
.
Here’s a minimal example:
# Print Underlined Text text = "abc" underlined_text = "\x1B[4m" + text + "\x1B[0m" print(underlined_text) print(text)
You can try this yourself in our interactive Jupyter notebook:
𧩠Interactive: Try it yourself in Google Colab
Note that this escape sequence will not work in all editors and IDEs. For example, I made it work in Jupyter Notebooks but not in my IDLE shell.
Let’s dive into some further explanations to see why this works next.
Some 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 underlined text
in Python using the ANSI escape sequence, we use: '\x1B[4m' + text + '\x1B[0m'
.
'\x1B[4m'
makes it underlined'\x1B[1m'
makes it bold'\x1B[1;4m'
makes it bold and underlined'\x1B[0m'
is the closing tag
So, you can chain together multiple text formatting specifiers by separating them with a semicolon. This is shown in the following example where the text is made bold and underlined:
Method 2: Make Text Bold and Underlined with Escape Sequence
Example 1: Escape-Sequence to print bold and underlined text for Windows Users
You may 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")
To make text bold and underlined, you can enclose the text in the escape sequence '\033[1;4m'
and '\033[0m'
.
'\x1B[1m'
makes it bold'\x1B[4m'
makes it underlined'\x1B[1;4m'
makes it bold and underlined'\x1B[0m'
is the closing tag
# Print Bold and Underlined Text print('\033[1;4m' + 'This text is bold and underlined' + '\033[0m')
Output:
π‘ NOTE: The code '\033[0m'
is used to end the bold and underlined text format. If you forget to add the ANSI code sequence to enclose the specific line of code, the following statements will also be printed in underlined format because you didn’t close the formatted special text.
Method 3: Using The simple_color Package
This is one of the easiest methods to print underlined 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 Finxters!')) # print underlined and colored text print('underlined: ', green('Welcome Finxter!', 'underlined')) # print italic and underlined and colored text print('Italic and Underlined: ', red('Welcome Finxter!', ['italic', 'underlined']))
Output:
Amazing! π€©
Method 4: 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 # Underlined Text text = colored('Hello and Welcome to FINXTER!', attrs=['underline']) print(text) # Underlined + Blue Text text2 = colored('This text will be printed in underlined and blue color', 'blue', attrs=['underline']) print(text2)
Output:
Method 5: Create an HTML Object
Prompt_toolkit
includes a
function that is compatible (as much as possible) with the built-in print_formatted_text()
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>
.
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:
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.
- 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!