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.
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)
fill(text, width = 70, **kwargs)
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)
text |
The text to de-indent. |
The function returns the de-indented string.
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))
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)
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.
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, "#"))
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.
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,))
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)
import textwrap
text = "All animals are equal but some animals are more equal than others."
print(textwrap.shorten(text, width = 40, placeholder = '....'))
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.