Daily "cool" textwrap, textwrap

Source: Internet
Author: User

Daily "cool" textwrap, textwrap

Introduction: You can use the textwrap module to format the text to be output in case of beautiful printing. This module allows you to improve functions such as automatic line feed or filling of paragraphs through programming.

1. Create instance data

1 sample_text = '''2     I’m very happy and I like to make friends with others. 3     I  also like singing but traveling is my favorite,   I have been to  many interesting places in China but I haven’t been to other  countries. 4     What a pity!At school,    I study Chinese,math, English, history, politics  and so on. I like all of them. 5     I often help my teacher take   care of my class and I think I am a good helper. 6     I live  with my parents and we go home on time every day.7     '''

Create the testwrap_example.py file and save the text of sample_text.
2. Fill a paragraph

1 import textwrap2 from testwrap_example import sample_text3 print 'No dedent: \n'4 print textwrap.fill(sample_text, width=50)

Running result:

The current situation is left alignment. Only the first line retains the indentation, and the intimidation in front of the other lines is embedded in the paragraph.

3. Remove existing indentation

1 import textwrap2 from testwrap_example import sample_text3 dedented_text = textwrap.dedent(sample_text)4 print 'Dedented: \n'5 print dedented_text

Running result:

Because "dedent" is the opposite of "indent", the result here is to get a text box and delete the leading blank area of each row. If one line is more locked than other lines, Zeng Hui has some blank characters not deleted. Even if:

Before Execution (! Space ):

! Aaa

!!! Aaa

! Bbb

After execution:

Aaa

!! Aaa

Bbb

4. Combined with dedent and fill

Now we will introduce the unindent text to fill () and provide a set of different wideh values (change the value to control the display width)

1 import textwrap2 from testwrap_example import sample_text3 dedented_text = textwrap.dedent(sample_text).strip()4 for width in [45,70]:5     print '%d  Columns:\n' % width6     print textwrap.fill(dedented_text, width=width)7     print 

Running result:

5. Suspension indent

Not only can the width of the output be set, but also the indent of the first line can be controlled separately to distinguish the back line

1 import textwrap2 from testwrap_example import sample_text3 dedented_text = textwrap.dedent(sample_text).strip()4 print textwrap.fill(dedented_text, 5                     initial_indent = '',6                     subsequent_indent = '*'*4,7                     width =75 )

Running result:

In this way, the indentions that have passed are generated, that is, the first line is different from other lines. indentions can contain spaces and other non-blank characters.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.