textwrap 模块

# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#textwrap 模块
#http://www.cnblogs.com/hongten/p/python_textwrap.html



import textwrap
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.
'''


#fill(text, width=70, **kwargs):
#该方法可以根据指定的长度,进行拆分字符串,然后逐行显示
print textwrap.fill(sample_text,width=20)



#wrap(text, width = 70, **kwargs):这个函数可以把一个字符串拆分成一个序列
#width,按照特定宽度拆分字符串。
print textwrap.wrap(sample_text,width=50)



#dedent()方法->文本进行不缩进显示,相应的indent()方法 -> 进行缩进显示
print textwrap.dedent(sample_text)
原文地址:https://www.cnblogs.com/dengyg200891/p/4943777.html