![]() |
|
[Tut] How to Print Underlined Text in Python? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Print Underlined Text in Python? (/thread-99413.html) |
[Tut] How to Print Underlined Text in Python? - xSicKxBot - 05-18-2022 How to Print Underlined Text in Python? 4.5/5 – (2 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:
So, without further delay, let the games begin! Problem Formulation: Given a string. How to print the string as underlined text in Python?[b]Method 1: Enclosing String in ANSI Escape Sequence ‘[b]\x1B[3m’[/b] and ‘[b]\x1B[0m’[/b][/b] The most straightforward way to print underlined text in Python is to enclose a given string Code: textCode: print("\x1B[4m" + text + "\x1B[0m")Here’s a minimal example: Code: # 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 ColabNote 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 Code: textCode: '\x1B[4m' + text + '\x1B[0m'
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 Code: os.system()Code: import os
os.system("color")To make text bold and underlined, you can enclose the text in the escape sequence Code: '\033[1;4m'Code: '\033[0m'
Code: # Print Bold and Underlined Text
print('\033[1;4m' + 'This text is bold and underlined' + '\033[0m')Output: ![]() NOTE: The code Code: '\033[0m'[b]Method 3: Using The [b]simple_color Package[/b][/b] This is one of the easiest methods to print underlined text in Python. The Code: simple_colorsYou 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 Code: simple_colorCode: simple_colorCode: pip install simple-colorsor, Code: python -m pip install simple-colorsAfter 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 Code: simple_colorsCode: 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! ![]() [b]Method 4: Using [b]termcolor Module[/b][/b] In Python, Code: termcolorThe 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: Code: termcolorCode: termcolorCode: pip install termcolorAfter installing the module, let’s visualize how you can use it to print the text in bold format. Example: Code: 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 Code: Prompt_toolkitCode: print_formatted_text()Code: print()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: Code: <b>Code: <i>Code: <u>Code: 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: ![]() [b]Conclusion[/b] 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.
Join the PyCharm Masterclass now, and master PyCharm by tomorrow! |