Fontstyle module in Python
Sometimes terminal text can be hard to read, the fontstyle module is package hosted on pypi.org for manipulating text. It can be used to break up the noise with some additional formatting, add colors, font weights and other styles to make it more readable. It also supports substring formatting for extra prettiness!
Installation:
pip install fontstyle
Features:
- Format text
- Preserve formatting
- Remove formatting
Methods provided by this module
1) fonstyle.apply(): This method adds formatting to the entire input argument string.
Syntax: fonstyle.apply(“STRING”,”all/possible/formating/options”)
- Available Colors: BLACK, BLUE, CYAN, DARKCYAN, GREEN, PURPLE, RED, YELLOW, WHITE
- BackGround of Text: BLACK_BG, BLUE_BG, CYAN_BG, GREEN_BG, PURPLE_BG, RED_BG, YELLOW_BG, WHITE_BG
- Formatting Arguments: ‘BLINK’, ‘BOLD’, ‘FAINT’, ‘HIDDEN’, ‘ITALIC’, ‘INVERSE’, ‘STRIKE’, ‘UNDERLINE’, ‘END’
2) fontstyle.erase(): This method is used to remove the formatting.
3) fontstyle.preserve(): This method returns the original text before formatting without removing the actual formatting of the text.
Below are some programs which depict the use of fontstyle module in Python:
Example 1:
Python3
# import moduleimport fontstyle # format texttext = fontstyle.apply('GEEKSFORGEEKS', 'bold/Italic/red/GREEN_BG') # display textprint(text) |
Output:

Here, we apply various formatting arguments like font color, background color, bold, italic on the given string.
Example 2:
Python3
# import required moduleimport fontstyle # display formatted textprint(fontstyle.apply('GEEKSFORGEEKS', 'bold/Italic/red/UNDERLINE/GREEN_BG')) print(fontstyle.apply('GEEKSFORGEEKS', 'bold/Italic/red/INVERSE/UNDERLINE/GREEN_BG')) |
Output:

Here is another example of how to format text using this module.
Example 3:
Python3
# import moduleimport fontstyle # apply formattingtext = fontstyle.apply( 'GEEKSFORGEEKS', 'bold/Italic/red/INVERSE/2UNDERLINE/GREEN_BG') # display textprint(text) # preserved textprint(fontstyle.preserve(text)) |
Output:

In this program, preserve() method is used to display the original text before formatting.
Note: You must specify which formatting you want to erase else it will erase last updated formatting
Example 4:
Python3
# import required moduleimport fontstyle # format texttext = fontstyle.apply( 'GEEKSFORGEEKS', 'bold/Italic/red/INVERSE/2UNDERLINE/GREEN_BG') # display textprint(text) # remove formattingtext = fontstyle.erase(a, 'bold/Italic/red/INVERSE/2UNDERLINE/GREEN_BG') # display original textprint(text) |
Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


