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.
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 |
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.
dedent(text)
copy
text |
The text to de-indent. |
The function returns the de-indented string.
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:
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. |
The function removes any existing known form of indentation, and instead applies the prefix as the indentation for each line.
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.
Shorten a text
We can use the textwrap.shorten()
function to truncate the given text to fit in the specified width.
shorten(text, width, **kwargs)
copy
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.