The textwrap module, which is a part of the Python Standard library, provides utilities that allows adjustments on where line breaks occur in a given piece of text. It can be used to break paragraphs into individual lines, format text for output in different ways, indent and de-indent text blocks, etc. 

It offers programmatic functionality similar to text wrapping or filling features found in text editors and word processors. 

Filling paragraphs

The module provides the fill() functions which wraps and formats a given paragraph by  adding line breaks in the text to make it fit in a given width.

ExampleEdit & Run
import textwrap


sample_text = "One of the great things about Python is its ability to interact with other languages and technologies. It can be used as a scripting language to automate the boring stuff, or to extend existing programs and applications with powerful new features."

#Filling paragraphs
filled_text = textwrap.fill(sample_text, width = 50)

print(filled_text)
copy
Output:
One of the great things about Python is its ability to interact with other languages and technologies. It can be used as a scripting language to automate the boring stuff, or to extend existing programs and applications with powerful new features. [Finished in 0.018774803262203932s]
Syntax:
fill(text, width = 70, **kwargs)
copy
text The text to be wrapped.
width An optional parameter which specifies the maximum length of each line of text. It defaults to 70.
**kwargs any other additional keyword arguments. used to modify the wrapping behavior
Parameters:

The function re-formats the 'text' to fit in lines of no more than 'width' columns. Tabs in the text  are expanded and other whitespace characters converted to space. It returns the resulting wrapped paragraph.

Removing indentation in a text

The textwrap.dedent() function removes any common leading whitespace from every line in the input string.

Syntax:
dedent(text)
copy
text The text to de-indent.
Parameters:

The function returns the de-indented string.

ExampleEdit & Run
import textwrap

#An indented piece of text
text = """
     One of the great things about Python
     is its ability to interact with
     other languages and technologies. """

#Use the dedent() function to remove leading whitespaces at each line.
print(textwrap.dedent(text))
copy
Output:
One of the great things about Python is its ability to interact with other languages and technologies.  [Finished in 0.01772055495530367s]

Indent a the lines in a given text

To indent a line in a text, we can use the textwrap.indent() function  The syntax for this function is as shown below:

Syntax:
textwrap.indent(text, prefix, predicate=None)
copy
text The piece of  text to be indented.
prefix The string to be added to the beginning of each line.
predicate An optional function to decide which lines to indent.
Parameters:

The function removes any existing known form of indentation, and instead applies the prefix as the indentation for each line. 

ExampleEdit & Run
import textwrap

source_string = """a = 10
b = 20
c = 30
print(a)
print(b)
print(c)"""
#Use the indent function to comment the whole 'python_code'
print(textwrap.indent(source_string, "#"))
copy
Output:
#a = 10 #b = 20 #c = 30 #print(a) #print(b) #print(c) [Finished in 0.01716683618724346s]

Hanging indents using fill()

Hanging indents are a formatting style where the first line of a paragraph is indented differently  from the subsequent lines. 

The fill() function, as we saw from its syntax above, takes additional keyword arguments. We can use these keyword arguments to achieve hanging indents, specifically , the initial_indent and subsequent_indent keyword arguments.

ExampleEdit & Run
import textwrap

#An irregularly indented piece of text
text = """Meet the Pynerds: an exclusive group of individuals who have dedicated their lives to perfect Python programming.
Hours upon hours are spent scouring the world wide web for the most eloquent expression of the Python language.          
Every keystroke must be perfect because to them, Python programming is an art form – and they strive to become masters at it.
But don't let their seriousness fool you: these Pynerds know how to have a good time too - as long as it involves coding!"""

#De-ident the text befor filling it
dedented_text = textwrap.dedent(text)

#Use the dedent function to remove indentation.
print(textwrap.fill(dedented_text, initial_indent = '  ', subsequent_indent = ' ' * 4, width = 50,))
copy
Output:
  Meet the Pynerds: an exclusive group of     individuals who have dedicated their lives to     perfect Python programming. Hours upon hours     are spent scouring the world wide web for the     most eloquent expression of the Python     language.           Every keystroke must be     perfect because to them, Python programming is     an art form – and they strive to become     masters at it. But don't let their seriousness     fool you: these Pynerds know how to have a     good time too - as long as it involves coding! [Finished in 0.018624864052981138s]

Shorten a text

We can use the textwrap.shorten() function to truncate the given text to fit in the specified width.

Syntax:
shorten(text, width, **kwargs)
copy
ExampleEdit & Run
import textwrap

text = "All animals are equal but some animals are more equal than others."

print(textwrap.shorten(text, width = 40, placeholder = '....'))
copy
Output:
All animals are equal but some.... [Finished in 0.0174765782430768s]

When using the shorten() function, the given text first has its whitespaces collapsed.  If it then fits in the *width*, it is returned as is. Otherwise, as many words as possible are joined and then the placeholder is appended.