本文主要是介绍textwrap模块1-fill(),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
fill()函数取文本作为输入,生成格式化的文本作为输出。
一、生成一个示例数据模块(textwrap_example.py)
sample_text = '''
The textwrap module can be used to format text for output in
situations where pretty-printing is desired. It offers
programmatic functionality similar to the paragraph wrapping
or filling features found in many text editors.
'''
二、利用fill函数进行格式化输出
import textwrap
from textwrap_example import sample_text
print 'No dedent:\n'
print textwrap.fill(sample_text,width=50)
结果:
No dedent:
The textwrap module can be used to format
text for output in situations where pretty-
printing is desired. It offers programmatic
functionality similar to the paragraph wrapping
or filling features found in many text editors.
如果width的值为60,
结果:
No
这篇关于textwrap模块1-fill()的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!