how to use python text wrap module?

Namaste Friends,



In Today's Post, I am going to show you how to use python textwrap module?

So, let's Start With Basic Queries.

Q 1. What is textwrap?
Ans. Textwrap is a Standard Python Module That Provides Us Special Facilities For Text Wrapping And Filling. In Simple Words, This Module Help Us To Manage Every Line Of Text String In Special Format with the use of tabs, spaces for adjusting width of lines in specific numbers of characters long.


Now, Let's Start Our Practical Examples Tutorials


Input : [1]  


#
# ==================================================
#          PYTHON Textwrap MODULE TUTORIAL
# ==================================================
# 
# author   : suraj singh bisht
# contact  : SSB
#            surajsinghbisht054@gmail.com
#            https://bitforestinfo.blogspot.com
#            
# Here, For This Tutorial
#
# I am using
#
# Operating System : Ubuntu 16.04
# Python Version   : python 2.7.12
# Editor           : ipython notebook
#

Output : [1]  

  

Input : [2]  

# import module
import textwrap

Output : [2]  

  

Input : [3]  

#
# Let Assume a Sample Paragraph
sample_text = """

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!


"""

Output : [3]  

  

Input : [4]  

#
# Example 1.
#
# Here, I am Going To Demonstrate textwrap.fill() function.
# This Function takes text as input and produces formatted 
# text as output. In Simple Words, This Function arrange and
# manage all text in a Specific Character Width. 
#
# 
for w in [25, 50, 75, 100]:
    print "\n\n\n\t Width : {}\n\n".format(w)
    print textwrap.fill(sample_text, width=w)

Output : [4]  

  Width : 25


  The Zen of Python, by
Tim Peters  Beautiful is
better than ugly.
Explicit is better than
implicit. Simple is
better than complex.
Complex is better than
complicated. Flat is
better than nested.
Sparse is better than
dense. Readability
counts. Special cases
aren't special enough to
break the rules. Although
practicality beats
purity. Errors should
never pass silently.
Unless explicitly
silenced. In the face of
ambiguity, refuse the
temptation to guess.
There should be one-- and
preferably only one
--obvious way to do it.
Although that way may not
be obvious at first
unless you're Dutch. Now
is better than never.
Although never is often
better than *right* now.
If the implementation is
hard to explain, it's a
bad idea. If the
implementation is easy to
explain, it may be a good
idea. Namespaces are one
honking great idea --
let's do more of those!



  Width : 50


  The Zen of Python, by Tim Peters  Beautiful is
better than ugly. Explicit is better than
implicit. Simple is better than complex. Complex
is better than complicated. Flat is better than
nested. Sparse is better than dense. Readability
counts. Special cases aren't special enough to
break the rules. Although practicality beats
purity. Errors should never pass silently. Unless
explicitly silenced. In the face of ambiguity,
refuse the temptation to guess. There should be
one-- and preferably only one --obvious way to do
it. Although that way may not be obvious at first
unless you're Dutch. Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a
bad idea. If the implementation is easy to
explain, it may be a good idea. Namespaces are one
honking great idea -- let's do more of those!



  Width : 75


  The Zen of Python, by Tim Peters  Beautiful is better than ugly. Explicit
is better than implicit. Simple is better than complex. Complex is better
than complicated. Flat is better than nested. Sparse is better than dense.
Readability counts. Special cases aren't special enough to break the rules.
Although practicality beats purity. Errors should never pass silently.
Unless explicitly silenced. In the face of ambiguity, refuse the temptation
to guess. There should be one-- and preferably only one --obvious way to do
it. Although that way may not be obvious at first unless you're Dutch. Now
is better than never. Although never is often better than *right* now. If
the implementation is hard to explain, it's a bad idea. If the
implementation is easy to explain, it may be a good idea. Namespaces are
one honking great idea -- let's do more of those!



  Width : 100


  The Zen of Python, by Tim Peters  Beautiful is better than ugly. Explicit is better than implicit.
Simple is better than complex. Complex is better than complicated. Flat is better than nested.
Sparse is better than dense. Readability counts. Special cases aren't special enough to break the
rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly
silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and
preferably only one --obvious way to do it. Although that way may not be obvious at first unless
you're Dutch. Now is better than never. Although never is often better than *right* now. If the
implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may
be a good idea. Namespaces are one honking great idea -- let's do more of those!

Input : [5]  

#
# Example 2.
#
# THis Function Work Completely Opposite To Our Previous
# Demonstrated Function. In Simple Words, This Function
# remove extra space characters from string.
# 
print textwrap.dedent(sample_text)

Output : [5]  

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Input : [6]  

# This Tutorial Ends Here,
#
# For Reference:
# 
#              Python Official Documentation
#
#            https://docs.python.org/2/library/textwrap.html
#
#




Thanks For Reading.

For More Update, Visit Our Blog Regularly. 
, Subscribe Our Blog, 
Follow Us and share it.
For Any Type of Suggestion, Help Or Question
Contact me:
S.S.B
surajsinghbisht054@gmail.com
or Comment Below

Share this

Related Posts

Previous
Next Post »