Python Text Wrapping and Filling

In python the textwrap module is used to format and wrap plain texts. There are some options to format the texts by adjusting the line breaks in the input paragraph.
To use these modules, we need to import the textwrap module in our code.
import textwrap

The Textwrapper instance attributes of the constructors are as follows −
width
The maximum length of lines. Default value is 70
expand_tabs
If the value of this attribute is true, then all tabs will be replaced by spaces. Default value is True.
tabsize
When the expand_tabs attribute is true, it will help to set the tabsize with different values. Default value is 8.
replace_whitespace
All whitespace characters in the text will be replaced by single space, when the value is set to True, Default value is True.
drop_whitespace
After wrapping the text, the whitespaces at the beginning and the end will be dropped. Default value is True.
initial_indent
It prepends the given string to the first line of the wrapped text. The default value is ‘ ’
subsequent_indent
It prepends the given string to all the lines of the wrapped text. The default value is ‘ ’
placeholder
It appends the string at the end of the output file whether it has been truncated. Default value is [...]
max_lines
This value will determine the number of lines will be there after wrapping the text. If the value is None, then there is no limit. Default value is None.
break_long_words
It breaks the long words to fit into given width. The default value is True.
break_on_hyphens
It is used to wrap the text after the hyphens for compound words. Default value is True.
There are some methods in the Textwrap module. These modules are −
Module (textwrap.wrap(text, width = 70, **kwargs)) −
This method wraps the input paragraph. It uses the line width to wrap the content. Default line width is 70. It returns a list of lines. In the list all wrapped lines are stored.
Module (textwrap.fill(text, width = 70, **kwargs)) − 
The fill() method is similar to the wrap method, but it does not generate a list. It generates a string. It adds the new line character after exceeding the specified width.
Module (textwrap.shorten(text, width, **kwargs)) −
This method shortens or truncates the string. After truncation the length of the text will be same as the specified width. It will add [...] at the end of the string.
 Live Demo
import textwrap

python_desc = """Python is a general-purpose interpreted, interactive, object-oriented, 
                 and high-level programming language. It was created by Guido van Rossum 
                 during 1985- 1990. Like Perl, Python source code is also available under 
                 the GNU General Public License (GPL). This tutorial gives enough 
                 understanding on Python programming language."""

my_wrap = textwrap.TextWrapper(width = 40)
wrap_list = my_wrap.wrap(text=python_desc)

for line in wrap_list:
   print(line)
    
single_line = """Python is a general-purpose interpreted, interactive, object-oriented, 
                 and high-level programming language."""

print('\n\n' + my_wrap.fill(text = single_line))

short_text = textwrap.shorten(text = python_desc, width=150)
print('\n\n' + my_wrap.fill(text = short_text))
Python is a general-purpose interpreted,
interactive, object-oriented,
and high-level programming language. It
was created by Guido van Rossum
during 1985- 1990. Like Perl, Python
source code is also available under
the GNU General Public License (GPL).
This tutorial gives enough
understanding on Python programming
language.

Python is a general-purpose interpreted,
interactive, object-oriented,
and high-level programming language.

Python is a general-purpose interpreted,
interactive, object-oriented, and high-
level programming language. It was
created by Guido van Rossum [...]