Open In App

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:

Methods provided by this module

1) fonstyle.apply(): This method adds formatting to the entire input argument string.



Syntax: fonstyle.apply(“STRING”,”all/possible/formatting/options”)

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:




# import module
import fontstyle
 
# format text
text = fontstyle.apply('GEEKSFORGEEKS', 'bold/Italic/red/GREEN_BG')
 
# display text
print(text)

Output:

Here, we apply various formatting arguments like font color, background color, bold, italic on the given string.

Example 2:




# import required module
import fontstyle
 
# display formatted text
print(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:




# import module
import fontstyle
 
# apply formatting
text = fontstyle.apply(
    'GEEKSFORGEEKS', 'bold/Italic/red/INVERSE/2UNDERLINE/GREEN_BG')
 
# display text
print(text)
 
# preserved text
print(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:




# import required module
import fontstyle
 
# format text
text = fontstyle.apply(
    'GEEKSFORGEEKS', 'bold/Italic/red/INVERSE/2UNDERLINE/GREEN_BG')
 
# display text
print(text)
 
# remove formatting
text = fontstyle.erase(a, 'bold/Italic/red/INVERSE/2UNDERLINE/GREEN_BG')
 
# display original text
print(text)

Output:


Article Tags :