Skip to content
Related Articles

Related Articles

Improve Article

Fontstyle module in Python

  • Last Updated : 12 Nov, 2020
Geek Week

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

 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




My Personal Notes arrow_drop_up
Recommended Articles
Page :