How to Print Italic Text in Python?

5/5 - (3 votes)

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:

The following video also mentions how to print italic 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 italic text in Python?

Method 1: Enclosing String in ANSI Escape Sequence \x1B[3m’ and \x1B[0m’

The most straightforward way to print italic text in Python is to enclose a given string text in the special ANSI escape sequence like so: print("\x1B[3m" + text + "\x1B[0m").

Here’s a minimal example:

# Print Italic Text
text = "abc"
italic_text = "\x1B[3m" + text + "\x1B[0m"
print(italic_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 italic text in Python using the ANSI escape sequence, we use: '\x1B[3m' + text + '\x1B[0m'.

  • '\x1B[3m' makes it italic
  • '\x1B[1m' makes it bold
  • '\x1B[1;3m' makes it bold and italic
  • '\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 italic:

Method 2: Make Text Bold and Italic with Escape Sequence

Example 1: Escape-Sequence to print bold and italic 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 italic, you can enclose the text in the escape sequence ‘\033[1;3m’ and ‘\033[0m’.

  • '\x1B[1m' makes it bold
  • '\x1B[3m' makes it italic
  • '\x1B[1;3m' makes it bold and italic
  • '\x1B[0m' is the closing tag
# Print Bold and Italic Text
print('\033[1;3m' + 'This text is bold and italic' + '\033[0m')

Output:

💡 NOTE: The code '\033[0m' is used to end the bold and italic 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 bold 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 italic, bold, and colored 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, italic, or even underline it using the simple_colors module.

from simple_colors import *

# normal and colored text
print('Normal:', blue('Welcome Finxters!'))

# print italic and colored text
print('italic: ', green('Welcome Finxter!', 'italic'))

# print italic and underlined and colored text
print('Italic and Underlined: ', red('Welcome Finxter!', ['italic', 'underlined']))

Output:

Looks amazing, doesn’t it?

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>.

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 italic texts in Python. Not only did we learn how to print italic texts, but we also learned how to style the code using colors and other formatting styles like underline, colored, and bold.

I hope this article helped you.

Please stay tuned and subscribe for more interesting articles!