05-18-2022, 06:23 AM
How to Print Underlined Text in Python?
<div><div class="kk-star-ratings kksr-valign-top kksr-align-left " data-payload="{"align":"left","id":"362193","slug":"default","valign":"top","reference":"auto","count":"2","readonly":"","score":"4.5","best":"5","gap":"5","greet":"Rate this post","legend":"4.5\/5 - (2 votes)","size":"24","width":"128","_legend":"{score}\/{best} - ({count} {votes})"}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 128px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend"> 4.5/5 – (2 votes) </div>
</div>
<p>You can change your text to <strong>bold</strong>, <em>italic</em>, and <span style="text-decoration: underline">underlined</span> 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. </p>
<p>Interesting! Isn’t it?</p>
<p>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. </p>
<p><strong>Related Tutorials:</strong></p>
<ul>
<li><a href="https://blog.finxter.com/how-to-print-bold-text-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-print-bold-text-in-python/" target="_blank" rel="noreferrer noopener">How to Print Bold Text in Python?</a></li>
<li><a href="https://blog.finxter.com/how-to-print-italic-text-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-print-italic-text-in-python/" target="_blank" rel="noreferrer noopener">How to Print Italic Text in Python?</a></li>
<li><a href="https://blog.finxter.com/how-to-print-underlined-text-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-print-underlined-text-in-python/" target="_blank" rel="noreferrer noopener">How to Print Underlined Text in Python?</a></li>
<li><a href="https://blog.finxter.com/how-to-print-colored-text-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-print-colored-text-in-python/" target="_blank" rel="noreferrer noopener">How to Print Colored Text in Python?</a></li>
</ul>
<p>So, without further delay, let the games begin! </p>
<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/2699.png" alt="⚙" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Problem Formulation</strong>: Given a string. How to print the string as <span style="text-decoration: underline">underlined</span> text in Python?</p>
<h2 id="method-2-using-the-ansi-escape-sequence-033-1m"><strong>Method 1: Enclosing String in ANSI Escape Sequence </strong> <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color">‘<strong>\x1B[3m’</strong></mark> and <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color">‘<strong>\x1B[0m’</strong></mark></h2>
<p class="has-global-color-8-background-color has-background">The most straightforward way to print underlined text in Python is to enclose a given string <code>text</code> in the special <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/ANSI_escape_code" data-type="URL" data-id="https://en.wikipedia.org/wiki/ANSI_escape_code" target="_blank">ANSI escape sequence</a> like so: <code>print("\x1B[4m" + text + "\x1B[0m")</code>. </p>
<p>Here’s a minimal example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Print Underlined Text
text = "abc"
underlined_text = "\x1B[4m" + text + "\x1B[0m"
print(underlined_text)
print(text)</pre>
<p>You can try this yourself in our interactive Jupyter notebook:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://colab.research.google.com/drive/1RFHKzzVS9Kwn1EEFYhp2gtjqArHJ6Cag?usp=sharing" target="_blank" rel="noopener"><img loading="lazy" width="525" height="168" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-158.png" alt="" class="wp-image-362350" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-158.png 525w, https://blog.finxter.com/wp-content/uplo...300x96.png 300w" sizes="(max-width: 525px) 100vw, 525px" /></a></figure>
</div>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f9e9.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Interactive</strong>: <a href="https://colab.research.google.com/drive/1RFHKzzVS9Kwn1EEFYhp2gtjqArHJ6Cag?usp=sharing" data-type="URL" data-id="https://colab.research.google.com/drive/1RFHKzzVS9Kwn1EEFYhp2gtjqArHJ6Cag?usp=sharing" target="_blank" rel="noreferrer noopener">Try it yourself in Google Colab</a></p>
<p>Note that this escape sequence will not work in all editors and IDEs. For example, I made it work in <a href="https://blog.finxter.com/survey-of-python-online-notebook-options/" data-type="post" data-id="324457" target="_blank" rel="noreferrer noopener">Jupyter Notebooks</a> but not in my <a href="https://blog.finxter.com/python-idle-vs-pycharm/" data-type="post" data-id="3345" target="_blank" rel="noreferrer noopener">IDLE</a> shell.</p>
<hr class="wp-block-separator"/>
<p>Let’s dive into some further explanations to see why this works next.</p>
<p>Some terminals support the capacity to pass in <strong>unique escape sequences</strong> to modify the <em>tone, color, and appearance</em> of the content being <a href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank" rel="noreferrer noopener">printed</a>. </p>
<p>These escape sequences are called <strong><span style="text-decoration: underline">ANSI escape sequences</span></strong> that got named after the ANSI standard that indicates their use. </p>
<p>Thus, you can utilize the built-in ANSI escape sequence to make the content or a specific text <strong>bold</strong>, <span style="text-decoration: underline">underlined</span>, <em>italic</em>, and even <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-purple-color">colored</mark>. To print the underlined <code><strong>text</strong></code> in Python using the ANSI escape sequence, we use: <code>'<strong>\x1B[4m' + text + '\x1B[0m'</strong></code>.</p>
<ul>
<li><code>'<strong>\x1B[4m'</strong></code> makes it underlined</li>
<li><code>'<strong>\x1B[1m'</strong></code> makes it bold</li>
<li><code>'<strong>\x1B[1;4m'</strong></code> makes it bold and underlined</li>
<li><code><strong>'\x1B[0m'</strong></code> is the closing tag</li>
</ul>
<p>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:</p>
<h2>Method 2: Make Text Bold and Underlined with Escape Sequence</h2>
<p class="has-medium-font-size"><strong>Example 1:</strong> Escape-Sequence to print <strong><span style="text-decoration: underline">bold and underlined</span></strong> text for Windows<em> </em>Users </p>
<p>You may have to call the <code data-enlighter-language="generic" class="EnlighterJSRAW">os.system()</code> module if you are using a Windows OS to make the ANSI escape sequence work properly.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import os
os.system("color")</pre>
<p>To make text bold and underlined, you can enclose the text in the escape sequence <code>'\033[1;4m'</code> and <code>'\033[0m'</code>. </p>
<ul>
<li><code>'<strong>\x1B[1m'</strong></code> makes it bold</li>
<li><code>'<strong>\x1B[4m'</strong></code> makes it underlined</li>
<li><code>'<strong>\x1B[1;4m'</strong></code> makes it bold and underlined</li>
<li><code><strong>'\x1B[0m'</strong></code> is the closing tag</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Print Bold and Underlined Text
print('\033[1;4m' + 'This text is bold and underlined' + '\033[0m')</pre>
<p><strong>Output:</strong></p>
<figure class="wp-block-image size-full"><a href="https://colab.research.google.com/drive/1RFHKzzVS9Kwn1EEFYhp2gtjqArHJ6Cag?usp=sharing" target="_blank" rel="noopener"><img loading="lazy" width="696" height="95" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-159.png" alt="" class="wp-image-362357" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-159.png 696w, https://blog.finxter.com/wp-content/uplo...300x41.png 300w" sizes="(max-width: 696px) 100vw, 696px" /></a></figure>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>NOTE: </strong>The code <code><strong>'\033[0m'</strong></code> 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.</p>
<h2 id="method-1-using-the-simple-color-package"><strong>Method 3: Using The <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color">simple_color</mark> Package</strong></h2>
<p>This is one of the easiest methods to print underlined text in Python. The <code>simple_colors</code> package includes many colors like <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">blue</mark>, black, <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-green-cyan-color">green</mark>, <mark style="background-color:rgba(0, 0, 0, 0);color:#ff00ff" class="has-inline-color">magenta</mark>, <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color">red</mark>, <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-amber-color">yellow</mark>, and <mark style="background-color:rgba(0, 0, 0, 0);color:#00ffff" class="has-inline-color">cyan</mark>. </p>
<p>You can also format your text in various styles like <strong>bold</strong>, <mark style="background-color:rgba(0, 0, 0, 0);color:#696969" class="has-inline-color">dim</mark>, <em>italic</em>, bright, <span style="text-decoration: underline">underlined</span>, reverse and blink that are included in the package.</p>
<p>Since the <code data-enlighter-language="generic" class="EnlighterJSRAW">simple_color</code> package isn’t a part of Python’s standard library; you need to <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-install-a-library-on-pycharm/" data-type="URL" data-id="https://blog.finxter.com/how-to-install-a-library-on-pycharm/" target="_blank">install</a> it before utilizing it. To install the <code>simple_color</code> package, copy the following code on your terminal:</p>
<pre class="wp-block-code"><code>pip install simple-colors</code></pre>
<p>or,</p>
<pre class="wp-block-code"><code>python -m pip install simple-colors</code></pre>
<p>After you have successfully installed the module, you can follow the syntax given in the example below to customize/style your code.</p>
<p><strong>Example:</strong> The following example demonstrates how you can add color, format, and make the text <strong>bold</strong> or even <span style="text-decoration: underline">underline</span> it using the <code data-enlighter-language="generic" class="EnlighterJSRAW">simple_colors</code> module. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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']))</pre>
<p><strong>Output:</strong></p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://colab.research.google.com/drive/1RFHKzzVS9Kwn1EEFYhp2gtjqArHJ6Cag?usp=sharing" target="_blank" rel="noopener"><img loading="lazy" width="837" height="276" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-160.png" alt="" class="wp-image-362360" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-160.png 837w, https://blog.finxter.com/wp-content/uplo...300x99.png 300w, https://blog.finxter.com/wp-content/uplo...68x253.png 768w" sizes="(max-width: 837px) 100vw, 837px" /></a></figure>
</div>
<p>Amazing! <img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f929.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2 id="method-3-using-termcolor-module"><strong>Method 4: Using <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color">termcolor</mark> Module</strong></h2>
<p>In Python, <code data-enlighter-language="generic" class="EnlighterJSRAW">termcolor</code> is a module utilized for the ANSII color formatting. </p>
<p>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. </p>
<p>Hence, we will use the <strong>bold</strong> <strong>property</strong> from the text attributes.</p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note:</strong> <code data-enlighter-language="generic" class="EnlighterJSRAW">termcolor</code> module isn’t a part of Python’s standard library. Thus, you need to install it before utilizing it. To install the <code data-enlighter-language="generic" class="EnlighterJSRAW">termcolor</code> module copy the following code on your terminal:</p>
<pre class="wp-block-code"><code>pip install termcolor</code></pre>
<p>After installing the module, let’s visualize how you can use it to print the text in bold format.</p>
<p><strong>Example:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)</pre>
<p><strong>Output:</strong></p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://colab.research.google.com/drive/1RFHKzzVS9Kwn1EEFYhp2gtjqArHJ6Cag?usp=sharing" target="_blank" rel="noopener"><img loading="lazy" width="932" height="244" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-161.png" alt="" class="wp-image-362368" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-161.png 932w, https://blog.finxter.com/wp-content/uplo...300x79.png 300w, https://blog.finxter.com/wp-content/uplo...68x201.png 768w" sizes="(max-width: 932px) 100vw, 932px" /></a></figure>
</div>
<h2 id="method-5-create-an-html-object">Method 5: Create an HTML Object </h2>
<p><code data-enlighter-language="generic" class="EnlighterJSRAW">Prompt_toolkit</code> includes a <code><code data-enlighter-language="generic" class="EnlighterJSRAW">print_formatted_text()</code></code> function that is compatible (as much as possible) with the built-in <code data-enlighter-language="raw" class="EnlighterJSRAW"><a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank">print()</a></code> function. It also supports colors and formatting.</p>
<p>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>, <code><i></code> and <code><u></code>. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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>'))</pre>
<p><strong>Output:</strong></p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="584" height="60" src="https://blog.finxter.com/wp-content/uploads/2021/03/image-83.png" alt="" class="wp-image-26989" srcset="https://blog.finxter.com/wp-content/uploads/2021/03/image-83.png 584w, https://blog.finxter.com/wp-content/uplo...300x31.png 300w" sizes="(max-width: 584px) 100vw, 584px" /></figure>
</div>
<h2 id="conclusion"><strong>Conclusion</strong></h2>
<p>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. </p>
<p>Please <strong><a href="https://www.youtube.com/channel/UCRlWL2q80BnI4sA5ISrz9uw" target="_blank" rel="noreferrer noopener">stay tuned</a></strong> and <strong><a href="http://blog.finxter.com/subscribe" target="_blank" rel="noreferrer noopener">subscribe</a></strong> for more interesting articles!</p>
<p class="has-text-align-center has-black-color has-luminous-vivid-amber-background-color has-text-color has-background" style="font-size:18px">Thank you, <strong>Rashi Agarwal</strong>, for helping me with this article. </p>
<figure class="wp-block-image is-resized"><a href="https://leanpub.com/c/thecompleteguidetopycharm"><img loading="lazy" src="https://dclrlzd4gl55u.cloudfront.net/2118/hero?1605640683" alt="The Complete Guide to PyCharm" width="359" height="202"/></a></figure>
<ul>
<li>Do you want to master the most popular Python IDE fast?</li>
<li>This course will take you from beginner to expert in PyCharm in ~90 minutes.</li>
<li>For any software developer, it is crucial to master the IDE well, to write, test and debug high-quality code with little effort.</li>
</ul>
<p><strong><a href="https://leanpub.com/c/thecompleteguidetopycharm" target="_blank" rel="noreferrer noopener">Join the PyCharm Masterclass</a></strong> now, and master PyCharm by tomorrow!</p>
</div>
https://www.sickgaming.net/blog/2022/05/...in-python/
<div><div class="kk-star-ratings kksr-valign-top kksr-align-left " data-payload="{"align":"left","id":"362193","slug":"default","valign":"top","reference":"auto","count":"2","readonly":"","score":"4.5","best":"5","gap":"5","greet":"Rate this post","legend":"4.5\/5 - (2 votes)","size":"24","width":"128","_legend":"{score}\/{best} - ({count} {votes})"}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 128px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend"> 4.5/5 – (2 votes) </div>
</div>
<p>You can change your text to <strong>bold</strong>, <em>italic</em>, and <span style="text-decoration: underline">underlined</span> 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. </p>
<p>Interesting! Isn’t it?</p>
<p>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. </p>
<p><strong>Related Tutorials:</strong></p>
<ul>
<li><a href="https://blog.finxter.com/how-to-print-bold-text-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-print-bold-text-in-python/" target="_blank" rel="noreferrer noopener">How to Print Bold Text in Python?</a></li>
<li><a href="https://blog.finxter.com/how-to-print-italic-text-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-print-italic-text-in-python/" target="_blank" rel="noreferrer noopener">How to Print Italic Text in Python?</a></li>
<li><a href="https://blog.finxter.com/how-to-print-underlined-text-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-print-underlined-text-in-python/" target="_blank" rel="noreferrer noopener">How to Print Underlined Text in Python?</a></li>
<li><a href="https://blog.finxter.com/how-to-print-colored-text-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-print-colored-text-in-python/" target="_blank" rel="noreferrer noopener">How to Print Colored Text in Python?</a></li>
</ul>
<p>So, without further delay, let the games begin! </p>
<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/2699.png" alt="⚙" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Problem Formulation</strong>: Given a string. How to print the string as <span style="text-decoration: underline">underlined</span> text in Python?</p>
<h2 id="method-2-using-the-ansi-escape-sequence-033-1m"><strong>Method 1: Enclosing String in ANSI Escape Sequence </strong> <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color">‘<strong>\x1B[3m’</strong></mark> and <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color">‘<strong>\x1B[0m’</strong></mark></h2>
<p class="has-global-color-8-background-color has-background">The most straightforward way to print underlined text in Python is to enclose a given string <code>text</code> in the special <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/ANSI_escape_code" data-type="URL" data-id="https://en.wikipedia.org/wiki/ANSI_escape_code" target="_blank">ANSI escape sequence</a> like so: <code>print("\x1B[4m" + text + "\x1B[0m")</code>. </p>
<p>Here’s a minimal example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Print Underlined Text
text = "abc"
underlined_text = "\x1B[4m" + text + "\x1B[0m"
print(underlined_text)
print(text)</pre>
<p>You can try this yourself in our interactive Jupyter notebook:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://colab.research.google.com/drive/1RFHKzzVS9Kwn1EEFYhp2gtjqArHJ6Cag?usp=sharing" target="_blank" rel="noopener"><img loading="lazy" width="525" height="168" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-158.png" alt="" class="wp-image-362350" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-158.png 525w, https://blog.finxter.com/wp-content/uplo...300x96.png 300w" sizes="(max-width: 525px) 100vw, 525px" /></a></figure>
</div>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f9e9.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Interactive</strong>: <a href="https://colab.research.google.com/drive/1RFHKzzVS9Kwn1EEFYhp2gtjqArHJ6Cag?usp=sharing" data-type="URL" data-id="https://colab.research.google.com/drive/1RFHKzzVS9Kwn1EEFYhp2gtjqArHJ6Cag?usp=sharing" target="_blank" rel="noreferrer noopener">Try it yourself in Google Colab</a></p>
<p>Note that this escape sequence will not work in all editors and IDEs. For example, I made it work in <a href="https://blog.finxter.com/survey-of-python-online-notebook-options/" data-type="post" data-id="324457" target="_blank" rel="noreferrer noopener">Jupyter Notebooks</a> but not in my <a href="https://blog.finxter.com/python-idle-vs-pycharm/" data-type="post" data-id="3345" target="_blank" rel="noreferrer noopener">IDLE</a> shell.</p>
<hr class="wp-block-separator"/>
<p>Let’s dive into some further explanations to see why this works next.</p>
<p>Some terminals support the capacity to pass in <strong>unique escape sequences</strong> to modify the <em>tone, color, and appearance</em> of the content being <a href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank" rel="noreferrer noopener">printed</a>. </p>
<p>These escape sequences are called <strong><span style="text-decoration: underline">ANSI escape sequences</span></strong> that got named after the ANSI standard that indicates their use. </p>
<p>Thus, you can utilize the built-in ANSI escape sequence to make the content or a specific text <strong>bold</strong>, <span style="text-decoration: underline">underlined</span>, <em>italic</em>, and even <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-purple-color">colored</mark>. To print the underlined <code><strong>text</strong></code> in Python using the ANSI escape sequence, we use: <code>'<strong>\x1B[4m' + text + '\x1B[0m'</strong></code>.</p>
<ul>
<li><code>'<strong>\x1B[4m'</strong></code> makes it underlined</li>
<li><code>'<strong>\x1B[1m'</strong></code> makes it bold</li>
<li><code>'<strong>\x1B[1;4m'</strong></code> makes it bold and underlined</li>
<li><code><strong>'\x1B[0m'</strong></code> is the closing tag</li>
</ul>
<p>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:</p>
<h2>Method 2: Make Text Bold and Underlined with Escape Sequence</h2>
<p class="has-medium-font-size"><strong>Example 1:</strong> Escape-Sequence to print <strong><span style="text-decoration: underline">bold and underlined</span></strong> text for Windows<em> </em>Users </p>
<p>You may have to call the <code data-enlighter-language="generic" class="EnlighterJSRAW">os.system()</code> module if you are using a Windows OS to make the ANSI escape sequence work properly.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import os
os.system("color")</pre>
<p>To make text bold and underlined, you can enclose the text in the escape sequence <code>'\033[1;4m'</code> and <code>'\033[0m'</code>. </p>
<ul>
<li><code>'<strong>\x1B[1m'</strong></code> makes it bold</li>
<li><code>'<strong>\x1B[4m'</strong></code> makes it underlined</li>
<li><code>'<strong>\x1B[1;4m'</strong></code> makes it bold and underlined</li>
<li><code><strong>'\x1B[0m'</strong></code> is the closing tag</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Print Bold and Underlined Text
print('\033[1;4m' + 'This text is bold and underlined' + '\033[0m')</pre>
<p><strong>Output:</strong></p>
<figure class="wp-block-image size-full"><a href="https://colab.research.google.com/drive/1RFHKzzVS9Kwn1EEFYhp2gtjqArHJ6Cag?usp=sharing" target="_blank" rel="noopener"><img loading="lazy" width="696" height="95" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-159.png" alt="" class="wp-image-362357" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-159.png 696w, https://blog.finxter.com/wp-content/uplo...300x41.png 300w" sizes="(max-width: 696px) 100vw, 696px" /></a></figure>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>NOTE: </strong>The code <code><strong>'\033[0m'</strong></code> 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.</p>
<h2 id="method-1-using-the-simple-color-package"><strong>Method 3: Using The <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color">simple_color</mark> Package</strong></h2>
<p>This is one of the easiest methods to print underlined text in Python. The <code>simple_colors</code> package includes many colors like <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">blue</mark>, black, <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-green-cyan-color">green</mark>, <mark style="background-color:rgba(0, 0, 0, 0);color:#ff00ff" class="has-inline-color">magenta</mark>, <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color">red</mark>, <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-amber-color">yellow</mark>, and <mark style="background-color:rgba(0, 0, 0, 0);color:#00ffff" class="has-inline-color">cyan</mark>. </p>
<p>You can also format your text in various styles like <strong>bold</strong>, <mark style="background-color:rgba(0, 0, 0, 0);color:#696969" class="has-inline-color">dim</mark>, <em>italic</em>, bright, <span style="text-decoration: underline">underlined</span>, reverse and blink that are included in the package.</p>
<p>Since the <code data-enlighter-language="generic" class="EnlighterJSRAW">simple_color</code> package isn’t a part of Python’s standard library; you need to <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-install-a-library-on-pycharm/" data-type="URL" data-id="https://blog.finxter.com/how-to-install-a-library-on-pycharm/" target="_blank">install</a> it before utilizing it. To install the <code>simple_color</code> package, copy the following code on your terminal:</p>
<pre class="wp-block-code"><code>pip install simple-colors</code></pre>
<p>or,</p>
<pre class="wp-block-code"><code>python -m pip install simple-colors</code></pre>
<p>After you have successfully installed the module, you can follow the syntax given in the example below to customize/style your code.</p>
<p><strong>Example:</strong> The following example demonstrates how you can add color, format, and make the text <strong>bold</strong> or even <span style="text-decoration: underline">underline</span> it using the <code data-enlighter-language="generic" class="EnlighterJSRAW">simple_colors</code> module. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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']))</pre>
<p><strong>Output:</strong></p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://colab.research.google.com/drive/1RFHKzzVS9Kwn1EEFYhp2gtjqArHJ6Cag?usp=sharing" target="_blank" rel="noopener"><img loading="lazy" width="837" height="276" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-160.png" alt="" class="wp-image-362360" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-160.png 837w, https://blog.finxter.com/wp-content/uplo...300x99.png 300w, https://blog.finxter.com/wp-content/uplo...68x253.png 768w" sizes="(max-width: 837px) 100vw, 837px" /></a></figure>
</div>
<p>Amazing! <img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f929.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2 id="method-3-using-termcolor-module"><strong>Method 4: Using <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color">termcolor</mark> Module</strong></h2>
<p>In Python, <code data-enlighter-language="generic" class="EnlighterJSRAW">termcolor</code> is a module utilized for the ANSII color formatting. </p>
<p>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. </p>
<p>Hence, we will use the <strong>bold</strong> <strong>property</strong> from the text attributes.</p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note:</strong> <code data-enlighter-language="generic" class="EnlighterJSRAW">termcolor</code> module isn’t a part of Python’s standard library. Thus, you need to install it before utilizing it. To install the <code data-enlighter-language="generic" class="EnlighterJSRAW">termcolor</code> module copy the following code on your terminal:</p>
<pre class="wp-block-code"><code>pip install termcolor</code></pre>
<p>After installing the module, let’s visualize how you can use it to print the text in bold format.</p>
<p><strong>Example:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)</pre>
<p><strong>Output:</strong></p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://colab.research.google.com/drive/1RFHKzzVS9Kwn1EEFYhp2gtjqArHJ6Cag?usp=sharing" target="_blank" rel="noopener"><img loading="lazy" width="932" height="244" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-161.png" alt="" class="wp-image-362368" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-161.png 932w, https://blog.finxter.com/wp-content/uplo...300x79.png 300w, https://blog.finxter.com/wp-content/uplo...68x201.png 768w" sizes="(max-width: 932px) 100vw, 932px" /></a></figure>
</div>
<h2 id="method-5-create-an-html-object">Method 5: Create an HTML Object </h2>
<p><code data-enlighter-language="generic" class="EnlighterJSRAW">Prompt_toolkit</code> includes a <code><code data-enlighter-language="generic" class="EnlighterJSRAW">print_formatted_text()</code></code> function that is compatible (as much as possible) with the built-in <code data-enlighter-language="raw" class="EnlighterJSRAW"><a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank">print()</a></code> function. It also supports colors and formatting.</p>
<p>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>, <code><i></code> and <code><u></code>. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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>'))</pre>
<p><strong>Output:</strong></p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="584" height="60" src="https://blog.finxter.com/wp-content/uploads/2021/03/image-83.png" alt="" class="wp-image-26989" srcset="https://blog.finxter.com/wp-content/uploads/2021/03/image-83.png 584w, https://blog.finxter.com/wp-content/uplo...300x31.png 300w" sizes="(max-width: 584px) 100vw, 584px" /></figure>
</div>
<h2 id="conclusion"><strong>Conclusion</strong></h2>
<p>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. </p>
<p>Please <strong><a href="https://www.youtube.com/channel/UCRlWL2q80BnI4sA5ISrz9uw" target="_blank" rel="noreferrer noopener">stay tuned</a></strong> and <strong><a href="http://blog.finxter.com/subscribe" target="_blank" rel="noreferrer noopener">subscribe</a></strong> for more interesting articles!</p>
<p class="has-text-align-center has-black-color has-luminous-vivid-amber-background-color has-text-color has-background" style="font-size:18px">Thank you, <strong>Rashi Agarwal</strong>, for helping me with this article. </p>
<figure class="wp-block-image is-resized"><a href="https://leanpub.com/c/thecompleteguidetopycharm"><img loading="lazy" src="https://dclrlzd4gl55u.cloudfront.net/2118/hero?1605640683" alt="The Complete Guide to PyCharm" width="359" height="202"/></a></figure>
<ul>
<li>Do you want to master the most popular Python IDE fast?</li>
<li>This course will take you from beginner to expert in PyCharm in ~90 minutes.</li>
<li>For any software developer, it is crucial to master the IDE well, to write, test and debug high-quality code with little effort.</li>
</ul>
<p><strong><a href="https://leanpub.com/c/thecompleteguidetopycharm" target="_blank" rel="noreferrer noopener">Join the PyCharm Masterclass</a></strong> now, and master PyCharm by tomorrow!</p>
</div>
https://www.sickgaming.net/blog/2022/05/...in-python/