Open In App
Related Articles

Fontstyle module in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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/formatting/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 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:

Python3

# 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:

Python3

# 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:

Python3

# 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:



Last Updated : 17 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads