The module textwrap is there for wrapping and formatting of plain text in python for data science. It provides a way to format text by simply adjusting the line breaks in the input paragraph
Textwrapper instance attributes –
- width – it refers to the maximum length of wrap lines. It has default value of 70.
- expand_tabs – It has a default value TRUE. In case the value is true then, all the tab characters in sample input gets expand to spaces by using this method.
- tabsize – It has a default value of 8. It is a method which expands all tab characters in the text to zero or more spaces. Which in turn, depends on the current column and given tab size, in case the value of expands_tabs is true.
- replace_whitespace – It has a default value TRUE. In case the value is true after the tab expansion but also before wrapping. Then the wrap method will replace each whitespace with single space. These whitespace characters are: tab, vertical tab, newline, carriage return and formfeed.
It has a default value true. If value is set to true, the whitespaces at the beginning and end of each line is dropp.
- Initial_indent – It has default value set to ‘ ‘. In this method, we prepend the given string to all lines of wrap output except the first.
- subsequent_indent – It has default value set to ‘ ‘.In this method, we prepend the given string to all lines of wrap output except the first.
- placeholder – It has default value set to ‘[…]’. In this method appending of the string is done at the end of the output text in case its turncate.
- max_lines – It has default set to None. In case the value is not set to None, the output text will contain at most max_lines lines which will have placeholders at the end of the output.
- break_long_words – It has default value set to True. When it is set to true, the words which are longer than the given width will be broken to fit every line in the given width.
- break_on_hyphens – It has a default value set to true. When the value is set to true, wrapping will occur on whitespaces and right after hyphens in compound words.
Functions by Textwrap module:
textwrap.wrap(text, width=70, **kwargs) – It is a function which wraps the input paragraph in such a way that each line in the paragraph is at most width characters long. The method returns a list of output lines. The return list is empty in case wrap out[ut has no content. Default width is set to 70.
textwrap.fill(text, width=70, **kwargs) –
This is similar to textwrap.wrap except for it retuens data which is join into single, newline-seperate string. In this function the input single paragraph is wrap in text and a single string containing the wrap paragraph is return.
- textwrap.dedent(text) – It is a function for removing all common leading whitespace from every line in the input text. It allows to use docstrings or embed multi-line strings line up with the left edge of the display, while deleting the formatting of the code.
textwrap.shorten(text, width, **kwargs) –
It is a function which truncates the input string so that, length of string becomes equal to given width. First of all the whitespaces are collapse in string by removing the whitespaces with single space. In case the modify string fits the given string, it is return otherwise, the characters from the end are drop off so, that the remaining words and placeholder fits within the width.
- textwrap.indent(text, prefix, predicate=None) – It is a function which is there to add given prefix at the beginning of select lines of text. The predicate argument controls which lines are to be indent.