TextWrap----Formatting text paragraphs

Source: Internet
Author: User

1.fill ()

Function: Takes text as input and generates formatted text as output.

#!/usr/bin/env pythonimport textwraptext = "China's Yaogan-27 remote sensing satellite was sent to space on Th     Ursday at 10:31 a.m. Beijing time, from Taiyuan launch site in Shanxi Province, north China. " Print Textwrap.fill (text,width=30)

Execution Result:

China ' s Yaogan-27 remote Sensing satellite

was sent to space on Thursday at 10:31 a.m.

Beijing time, from Taiyuan launch site in

Shanxi Province, north China.


China ' s Yaogan-27 remote

Sensing satellite was

Sent into space on Thursday at

10:31 a.m. Beijing time,

From Taiyuan launch site in

Shanxi Province, north China.

Only the first line of text is indented, and the spaces in front of the remaining lines are embedded in the paragraph.

2.dedent ()

Action: Remove indent, delete previous whitespace

#!/usr/bin/env pythonimport textwraptext = "China's Yaogan-27 remote sensing satellite was sent to space on Th     Ursday at 10:31 a.m. Beijing time, from Taiyuan launch site in Shanxi Province, north China. " Dedent_text = textwrap.dedent (text) print Textprint Dedent_text

Execution Result:

China's Yaogan-27 remote sensing satellite was sent to space on Thursday at 10:31 a.m. Beijing time, from Taiyuan launch site in Shanxi Province, North China.china ' s Yaogan-27 Remote Sensing satellite was Sent into space on Thursday at 10:31 a.m. Beijing time, from Taiyuan launch site in Shanxi Province, north China.

3. Combine dedent and fill

#!/usr/bin/env pythonimport textwraptext = "China's Yaogan-27 remote sensing satellite was sent to space on Th     Ursday at 10:31 a.m. Beijing time, from Taiyuan launch site in Shanxi Province, north China. " Print Textdedent_text = textwrap.dedent (text). Strip () for width in [30,50]: print '%d columns:\n '% width print textw Rap.fill (dedent_text,width=width) print

Execution Result:

China's Yaogan-27 remote sensing satellite was sent to space on Thursday at 10:31 a.m. Beijing time, from Taiyuan launch site in Shanxi Province, North china.30 Columns:china ' s Yaogan-27 remotesensing sate  Llite was Sentinto space on Thursday at10:31 a.m.. Beijing time, Fromtaiyuan launch site in Shanxiprovince, North china.50 Columns:china ' s Yaogan-27 Remote Sensing Satellit  e wassent into space on Thursday at 10:31 a.m. Beijingtime, from Taiyuan launch site in Shanxiprovince, north China.

4. Hanging indent

You can control the indentation of the first line

#!/usr/bin/env pythonimport textwraptext =  '     china ' s  yaogan-27 remote sensing satellite     was sent into  space on thursday at 10:31 a.m.     beijing time,  From taiyuan launch site in     shanxi province, north  china. " Print textdedent_text = textwrap.dedent (text). Strip () for width in [30,50]:     print  '%d columns:\n '  % width    print  Textwrap.fill (dedent_text,                         initial_indent= ',                          subsequent_Indent= '   '  * 4,                         width=width,)     print

Execution Result:

    china ' s yaogan-27 remote sensing satellite      was sent into space on Thursday at 10:31 a.m.      Beijing Time, from Taiyuan launch site in      shanxi province, north china.30 columns:china ' s yaogan-27 remote     sensing satellite  was    sent into space  On    thursday at 10:31 a.m.    beijing time,  from Taiyuan    launch site in  Shanxi     province, north china.50 columns:china ' s yaogan-27 remote sensing  satellite  was    sent into space on thursday at  10:31 a.m.  &nbSp; beijing time, from taiyuan launch site in    shanxi  province, north china.

The hanging indent can be preceded by a prefix * and other symbols

#!/usr/bin/env pythonimport textwraptext =  '     china ' s  yaogan-27 remote sensing satellite     was sent into  space on thursday at 10:31 a.m.     beijing time,  From taiyuan launch site in     shanxi province, north  china. " Print textdedent_text = textwrap.dedent (text). Strip () for width in [30,50]:     print  '%d columns:\n '  % width    print  Textwrap.fill (dedent_text,                         initial_indent= ' * ',                          subsequent_indent= ' * '  * 4,                         width=width,)     print

Execution Result:

China's Yaogan-27 remote sensing satellite was sent to space on Thursday at 10:31 a.m.  Beijing time, from Taiyuan launch site in Shanxi Province, North china.30 Columns:*china ' s Yaogan-27 remote****sensing Satellite was****sent into space on****thursday @ 10:31 a.m.****beijing time, from Taiyuan****launch site in shanxi***  *province, North china.50 Columns:*china ' s Yaogan-27 Remote sensing satellite was****sent to space on Thursday at 10:31 A.m.****beijing time, from Taiyuan launch site In****shanxi province, north China.

Remark: Indent (): Action indent, opposite to Dedent ()

TextWrap----Formatting text paragraphs

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.