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