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 −

Sr.No. Attribute & Description
1

width

The maximum length of lines. Default value is 70

2

expand_tabs

If the value of this attribute is true, then all tabs will be replaced by spaces. Default value is True.

3

tabsize

When the expand_tabs attribute is true, it will help to set the tabsize with different values. Default value is 8.

4

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.

5

drop_whitespace

After wrapping the text, the whitespaces at the beginning and the end will be dropped. Default value is True.

6

initial_indent

It prepends the given string to the first line of the wrapped text. The default value is ‘ ’

7

subsequent_indent

It prepends the given string to all the lines of the wrapped text. The default value is ‘ ’

8

placeholder

It appends the string at the end of the output file whether it has been truncated. Default value is […]

9

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.

10

break_long_words

It breaks the long words to fit into given width. The default value is True.

11

break_on_hyphens

It is used to wrap the text after the hyphens for compound words. Default value is True.

Text Wrapping Methods

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.

Example Code

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))

Output

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 [...]

Updated on: 30-Jul-2019

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements