How To Underline Text In Python?

How To Underline Text In Python?

Introduction

To Underline Text In Python, the Unicode character "\u0332", acts as an underline on the character that precedes it in a string. So, if you put any string after this Unicode, the text will be printed underlined in Python.

Example 1

Underline Text In Python using join() function.

def underline(text):
    print("\u0332".join(text))
    print("\u0332".join("How are you?"))
    
underline("Hello reader!")

Output:

H̲e̲l̲l̲o̲ ̲r̲e̲a̲d̲e̲r̲!
H̲o̲w̲ ̲a̲r̲e̲ ̲y̲o̲u̲?

Example 2

You can try the ANSI escape sequences method as well to Underline Text In Python from this answer on StackOverflow.

class Format:
    end = '\033[0m'
    underline = '\033[4m'

print(Format.underline + 'Your text here' + Format.end)

Output:

Your text here

We hope these examples will help you to learn how to print underlined text in Python.

Thank you for reading this article, click here to start learning Python in 2022.


Also Read:

Share:
Avatar of Harry

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you