How to Use Textwrap Module in Python

This article will cover a guide on using the “textwrap” module in Python. As the name suggests, this module can be used to “wrap” text so that lines or sentences can be fit within the predefined length constraints. This is usually done by shortening a piece of text and moving the longer parts to the next line so that all lines adhere to the character limits. The usage of the textwrap module can be best understood through examples. Below are some code samples that illustrate the usage of the textwrap module and its methods. These code samples are tested with Python 3.9.5 on Ubuntu 21.04.

Wrapping Text Using a Character Limit Threshold

Have a look at the code sample below:

import textwrap

para = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'''
wrapped_para= textwrap.wrap(para)
print (wrapped_para)
for line in wrapped_para:
    print (line, len(line))

The first statement imports the “textwrap” module. The “para” variable contains a piece of text that will be wrapped into multiple lines. Next the “wrap” method from the textwrap module is called by supplying the para variable as the argument. This method is used to wrap and divide text into multiple lines. The two “print” statements show the output of wrapped text.

After running the above code sample, you should get the following output:

['Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do', 'eiusmod tempor incididunt ut labore et dolore magna aliqua.']
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do 63
eiusmod tempor incididunt ut labore et dolore magna aliqua. 59

The wrap method returns a list of segmented para, as shown in the first line of the output. You can run a variety of operations on the list to present the text any way you want. The last two lines in the output show the output of individual segmented lines and their character count. By default, the wrap method applies a character limit of 70 characters. You can change this limit by supplying an extra “width” argument with your own custom value, as shown in the code sample below:

import textwrap

para = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'''
wrapped_para= textwrap.wrap(para, width=90)
print (wrapped_para)
for line in wrapped_para:
    print (line, len(line))

With the character limit of 90 characters now, the code sample above now produces a different output:

['Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt', 'ut labore et dolore magna aliqua.']
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt 89
ut labore et dolore magna aliqua. 33

Using the Fill Method to Produce a Chunk of Wrapped Text

If you don’t want a list of segmented text and directly want to use the wrapped multi-line text, you can use the “fill” method available in the textwrap module.

import textwrap

para = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'''
wrapped_para= textwrap.fill(para, width=90)
print (wrapped_para)

After running the code above code sample, you should get the following output:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.

The fill method separates each segmented line with a “\n” newline character. It is a convenience method included in the textwrap module. You can achieve the same effect using the wrap method by joining the segmented lines in the list using the “\n” character.

import textwrap

para = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'''
wrapped_para= "\n".join(textwrap.wrap(para, width=90))
print (wrapped_para)

Shortening or Truncating the Text

You can truncate or shorten the text using the “shorten” method available in the textwrap module. It will cut the text upto a particular character limit specified as an argument. At the end of the text, three dots (ellipsis) will be added to indicate that this is just a short summary of the text and not a full paragraph or sentence. Note that the shorten method may not work properly with texts that do not contain proper space separated words. If there are multiple whitespaces between words, they will be reduced to a single whitespace.

Take a look at the code sample below:

import textwrap

para = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'''
wrapped_para= textwrap.shorten(para, width=90)
print (wrapped_para)

The code is pretty straightforward. You call the shorten method by supplying the para variable as the mandatory argument and specify a character limit. After running the above code sample, you should get the following output:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor […]

Add and Remove Indentation

You can use “dedent” method to remove and “indent” method to add leading indentation to each line in a piece of text. The code sample below shows usage of dedent method:

import textwrap

para = '''  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'''

print (para)
wrapped_para= textwrap.dedent(para)
print (wrapped_para)

The para variable now contains two lines, each with a leading indentation of a tab character (around four spaces). String wrapped in triple quotes preserve spacing and these strings are presented “as is” without any modifications and without escaping of special characters. The dedent method is then called by supplying it the para variable as an argument. After running the above code sample, you should get the following output:

    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

The first two lines show the original text with leading indentations. The last two lines in output show the same two lines with leading indentations removed.

The “indent” method works in a similar way, but now you have to specify a leading indentation string or a prefix as an extra argument.

import textwrap

para = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'''

print (para)
wrapped_para= textwrap.indent(para, "\t")
print (wrapped_para)

Here the tab character “\t” is used to add a tab before each line in the text. After running the above code sample, you should get the following output:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Conclusion

The textwrap module provides a number of convenient methods allowing you to shorten text in a variety of ways. Using these methods on paragraphs can improve their formatting and readability, especially in user interfaces with space constraints.



from Linux Hint https://ift.tt/2UN00Ko

Post a Comment

0 Comments