# Undertake software automation implementation, training, and other gtalk: ouyangchongwu # gmail. comqq 37391319
# Copyright. For reposted content, please contact us by letter
# Shenzhen testing automation python project receiving group 113938272 Shenzhen accounting software testing part-time job 6089740
# Shenzhen dashboard group 66250781 wugang DongKou Chengbu Xinning township sentiment group 49494279
Instance text:
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 in1_text editors.
'''
Paragraph filling:
Import textwrap
From textwrap_exampleimport sample_text
Print 'nodedent: \ N'
Printtextwrap. fill (sample_text, width = 50)
Execution result:
# Pythontextwrap_fill.py
No dedent:
The textwrap module can be used to format
Text for outputin situations where pretty-
Printing is desired. It offers programmatic
Functionalitysimilar to the paragraph wrapping
Or fillingfeatures found in invalid text editors.
The result is left aligned and the first line is indented. The space in the row is retained.
Remove indent:
Import textwrap
Fromtextwrap_example import sample_text
Dedented_text = textwrap. dedent (sample_text)
Print 'dedented :'
Printdedented_text
Execution result:
# Pythontextwrap_dedent.py
Dedented:
The textwrapmodule can be used to format text for output in
Situations wherepretty-printing is desired. It offers
Programmaticfunctionality similar to the paragraph wrapping
Or fillingfeatures found in invalid text editors.
In this way, the first line will not be indented.
Combined with removing indentation and filling:
Import textwrap
Fromtextwrap_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
45 Columns:
The textwrapmodule can be used to format
Text for output insituations where pretty-
Printing isdesired. It offers programmatic
Functionalitysimilar to the paragraph
Wrapping orfilling features found in detail
Text editors.
70 Columns:
The textwrapmodule can be used to format text for output in
Situations wherepretty-printing is desired. It offersprogrammatic
Functionality similarto the paragraph wrapping or filling features
Found in your texteditors.
Suspension indent: the first line of the suspension indent is smaller than the indentation of other lines.
Import textwrap
Fromtextwrap_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.py
The textwrapmodule can be used to format text
Output in situations where pretty-printingis
Desired. It offers programmatic functionality
Similar to the paragraph wrapping orfilling
Features found in your text editors.
'* 4 can also be replaced by other characters.
TextWrap provides the wrap () and fill () functions, as well as the TextWrapper class, tool functions dedent (). Usually wrap or fill in one or two strings using wrap () and fill (). In other cases, TextWrapper is more efficient.
Textwrap. wrap (text [, width [,...])
Wrap a single paragraph (text is input, a string), and the maximum width of each line is width. Returns the list of output rows without line breaks. Width: 70 by default.
Textwrap. fill (text [, width [,...])
Wrap a Text Segment and return a string containing the section. It is actually the abbreviation of "\ n". join (wrap (text. Wrap () and fill () create a TextWrapper instance and call a method. These instances are not reused, so it is more effective to wrap/populate many text strings to construct your own TextWrapper object. TextWrapper. break_long_words: Specifies whether to split long words.
Textwrap. dedent (text)
Unindent removes the blank at the beginning of each line. This allows you to easily display the content in the three quotation marks without modifying the indentation in the source code.