dev_neil_a
Posted on May 1, 2023
- Introduction
- Method One: Built-In Python Text Styling
- Method Two: Text Styling Using Colored
- Conclusion
Introduction
Python, along with many other languages, the output to the terminal can be customized to add both color and styling, such as bolding and underlining of text.
In this how-to, I'll be highlighting two methods that can be used to add both coloring and styling, along with a examples for each method. There are other options available but I'll cover the two main methods I typically use.
Let us get started with the first method.
Method One: Built-In Python Text Styling
The first method is to use what is already available within Python. The benefit of this method is that nothing, other than Python, needs to be installed for it to work. So, let's start with an example.
This first example will set the text to be bold, green and be shown on a black background (\
is used to break the code up onto separate lines (excluding the first one before 033)):
# --- Display a bold line of green text with a black background:
print("\033[1;32;40m\
Bold (1) green (32) text on a black background(40)")
Output:
To breakdown what was done to make this happen, it is this part (\033[1;32;40m
) of the code that performs the formatting of the text.
\033[ # --- This is an escape character.
1; # --- This implies that the text should be bold.
32; # --- This implies that the text should be green.
40m # --- This implies that the background should be black.
Let's take another example, this time the font color will be cyan, underlined and styled normally:
# --- Display a normal, underlined line of cyan text with a black background:
print("\033[4;36;40m\
Normal, underlined (4) cyan text (36) on a black background (40)")
Output:
Let's break this code down:
\033[ # --- This is an escape character.
4; # --- This implies that the text should be underlined and normal.
36; # --- This implies that the text should be cyan.
40m # --- This implies that the background should be black.
How can one determine which codes to use for each part? There is an excellent guide for what is available for each part on this website. It shows the formatting options and the color codes that are available.
One of the main limitations of this method is that both the font color and background color are limited to eight possibilities. As a result, it may not offer the styling that is required. A way to get around this is to use a library or package, such as colored which we will look at next.
Method Two: Text Styling Using Colored
Colored is a library that can be installed to both simplify and expand the formatting options for the terminal text beyond what is available in Python. One major benefit is that it supports more than eight colors.
First of all, as it's not part of Python, it will need to be installed using pip:
pip install colored
Now that it is installed, let's take a look at an example of how to use it:
# --- Import colored:
from colored import fg, bg, attr
# --- Define the colors and styles to use:
text_color = bg("#FFFFFF") + fg("#FF0000")
text_style = attr("bold") + attr("underlined")
text_reset = attr("reset") # Resets the terminal color / style back to its default.
# --- Display a line of bold, underlined text in red
# --- with a white background:
print(text_color + text_style + \
"Bold, underlined red text on a white background" + \
text_reset)
Output:
That is a bit more code than what was used in method one so let's break it down.
First, the colored library was imported with the specific functions of fg, bg and attr
being specified for import. What does each one do?
-
bg
is used to set the background ground color. -
fg
is used to set the foreground (font) color. -
attr
is used to set the font style, such as underlining the text.
Both bg
and fg
can use hex codes for colors, but they can also use a list of pre-defined names, such as red for example. There are also other options that can be used instead of bg
and fg
but for this guide, those were chosen.
Next, three variables were defined to set the background & foreground (text_color
), style (text_style
) and lastly text_reset
to reset the terminal back to it's default font style.
Finally, the last part of the code is the text to show in the terminal. The text_color
and text_style
must go in front of the text to be formatted with those variables, otherwise they won't be applied. After the text to be displayed, text_reset
is then used to reset the terminal font style back to it's default. If this was omitted, any text that is shown after would have the same styling.
Now, let's look at one final example. This time, the text will have two colors that can be used. The process is very much the same but with an additional color variable:
# --- Import colored:
from colored import fg, bg, attr
# --- Define the colors and styles to use
text_color_one = bg("#FFFFFF") + fg("#FF0000") # Red text, black background
text_color_two = bg("#FFFFFF") + fg("#0000FF") # Blue text, black background
text_style = attr("bold") + attr("underlined")
text_reset = attr("reset") # Resets the terminal color / style back to its default.
# # --- Display a line of bold, underlined text in red
# # --- with a white background and the word 'color' being blue:
print(f"\n{text_color_one}{text_style}Bold, underlined mixed \
{text_color_two}{text_style}color \
{text_color_one}{text_style}text on a white background\
{text_reset}")
Output:
In this example, the only differences were:
- An f-string was used (my preference for putting variables in strings)
- The word 'colour' was in blue and because the text after 'color' needed to be in red,
text_color_one
had to be specified a second time. If it wasn't, the rest of the text would have been blue.
For further details of how to use the colored library, please see the documentation on pypi.org.
Conclusion
As I mentioned previously, these are just two options that are available to style text in a terminal with Python. There are plenty of other options available so by all means have a look at those other options and perhaps one of them might be better suited to your needs.
Thank you for reading this article and I hope it was of use to you. Have a nice day!
Posted on May 1, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.