Python Module Learning-textwrap text wrapping and padding
code example:
Sample_text = "'
The TextWrap module can beused to format text for output in
Situations wherepretty-printing is desired. It offers
Programmatic functionalitysimilar to the paragraph wrapping
or filling features found Inmany text editors.
'''
Paragraph fill:
Import textwrapfrom textwrap_exampleimport sample_text print ' nodedent:\n ' Printtextwrap.fill (sample_text, Width =50)
Execution Result:
# pythontextwrap_fill.py
No dedent:
The TextWrap module can used to format
Text for outputin situations where pretty-
Printing is desired. It offers programmatic
Functionalitysimilar to the paragraph wrapping
Or fillingfeatures found in many text editors.
The result is left-aligned and the first line has indentation. The spaces in the row continue to persist.
To remove indents:
Import textwrapfromtextwrap_example Import sample_text dedented_text = textwrap.dedent (sample_text) print ' dedented: ' Printdedented_text
Execution Result:
# pythontextwrap_dedent.py
Dedented:
The textwrapmodule can used to format text for output in
Situations wherepretty-printing is desired. It offers
Programmaticfunctionality similar to the paragraph wrapping
Or fillingfeatures found in many text editors.
This way the first line is not indented.
To remove indents and fills in combination:
Import textwrapfromtextwrap_example Import Sample_text dedented_text =textwrap.dedent (sample_text). Strip () for width in [45,70]: print '%d columns:\n '% width print textwrap.fill (dedented_text,width=width) Print
Execution Result:
# pythontextwrap_fill_width.py
Columns:
The textwrapmodule can used to format
Text for output insituations where pretty-
Printing isdesired. It offers programmatic
Functionalitysimilar to the paragraph
Wrapping orfilling features found in many
Text editors.
Columns:
The textwrapmodule can used to format text for output in
Situations wherepretty-printing is desired. It offersprogrammatic
Functionality Similarto the paragraph wrapping or filling features
Found in many texteditors.
Hanging indent: The indentation of the first line of the hanging indent is less than the indentation of the other lines.
Import textwrapfromtextwrap_example Import Sample_text dedented_text =textwrap.dedent (sample_text). Strip () Printtextwrap.fill (Dedented_text, initial_indent= ", subsequent_indent=" * 4, width=50, ) Execution Result: # pythontextwrap_hanging_indent.pythe Textwrapmodule can be used to format text for output in situations where PR Etty-printingis desired. It offers programmatic functionality similar to the paragraph wrapping orfilling features found in many text Edito Rs.
The "* * *" can also be replaced with other characters.
TextWrap provides the function wrap () and fill (), as well as the Textwrapper class, tool function Dedent (). Usually wraps or fills one or two strings using wrap () and fill (). Other situations use textwrapper more efficiently.
Textwrap.wrap (text[,width[, ...])
Wraps a single paragraph (text is input, string), the longest width of each line. Returns a list of output rows, with no newline characters in the last line. Width default 70.
Textwrap.fill (text[,width[, ...])
Wraps a single paragraph of text and returns a string containing the paragraph of the parcel. is actually "\ n". Join (Wrap (text,...)) The abbreviation. Wrap () and fill () creates a Textwrapper instance and invokes a method. These instances are not reused, so it is more efficient to wrap/populate many text strings to construct their own Textwrapper objects. Textwrapper.break_long_words sets whether to split long words.
Textwrap.dedent (text)
The anti-indentation removes the white space at the beginning of each line. This facilitates displaying the contents of the three quotation marks without modifying the indentation in their source code.