在看python的API的时候,发现python的textwrap在处理字符串样式的时候功能强大

在这里我做了一个demo:

textwrap提供了一些方法:

wrap(text, width = 70, **kwargs):这个函数可以把一个字符串拆分成一个序列
 from textwrap import *

 #使用textwrap中的wrap()方法
def test_wrap():
test_str = '''\
The textwrap module provides two convenience functions, wrap() and fill(), as well as 1
TextWrapper, the class that does all the work, and two utility functions, dedent() and indent(). If 2
you’re just wrapping or filling one or two text strings, the convenience functions should be good 3
enough; otherwise, you should use an instance of TextWrapper for efficiency. 4
'''
print(wrap(test_str, 20)) def main():
test_wrap() if __name__ == '__main__':
main()

输出效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
[' The textwrap', 'module provides two', 'convenience', 'functions, wrap()', 'and fill(), as well', 'as 1', 'TextWrapper, the', 'class that does all', 'the work, and two', 'utility functions,', 'dedent() and', 'indent(). If 2', 'you’re just wrapping', 'or filling one or', 'two text strings,', 'the convenience', 'functions should be', 'good 3 enough;', 'otherwise, you', 'should use an', 'instance of', 'TextWrapper for', 'efficiency. 4']
>>>

我们会发现,wrap()函数,把字符串拆分成了一个序列,在这个序列中,每个元素的长度是一样的。

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

 #fill()方法
def test_wrap():
test_str = '''\
The textwrap module provides two convenience functions, wrap() and fill(), as well as 1
TextWrapper, the class that does all the work, and two utility functions, dedent() and indent(). If 2
you’re just wrapping or filling one or two text strings, the convenience functions should be good 3
enough; otherwise, you should use an instance of TextWrapper for efficiency. 4
'''
print(fill(test_str, 40)) def main():
test_wrap() if __name__ == '__main__':
main()

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
The textwrap module provides two
convenience functions, wrap() and
fill(), as well as 1 TextWrapper,
the class that does all the work, and
two utility functions, dedent() and
indent(). If 2 you’re just wrapping
or filling one or two text strings, the
convenience functions should be good 3
enough; otherwise, you should use an
instance of TextWrapper for efficiency.
4
>>>
dedent()方法->文本进行不缩进显示,相应的indent()方法 -> 进行缩进显示
 from textwrap import *

 #dedent()方法
def test_wrap():
test_str = '''\
The textwrap module provides two convenience
functions, wrap() and fill(), as well as 1
TextWrapper, the class that does all the work,
and two utility functions, dedent() and indent(). If 2
you’re just wrapping or filling one or two text strings,
the convenience functions should be good 3
enough; otherwise, you should use an instance
of TextWrapper for efficiency. 4
'''
print(repr(dedent(test_str))) def main():
test_wrap() if __name__ == '__main__':
main()

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
'The textwrap module provides two convenience\n functions, wrap() and fill(), as well as 1\nTextWrapper, the class that does all the work,\n and two utility functions, dedent() and indent(). If 2\nyou’re just wrapping or filling one or two text strings,\n the convenience functions should be good 3\nenough; otherwise, you should use an instance\n of TextWrapper for efficiency. 4\n'
>>>

python开发_textwrap文本样式的更多相关文章

  1. 前端开发CSS3——文本样式和盒子及样式

    博主废话少说,直接介绍css常用的属性和属性值:属性和值只需过一遍,页面的结构还是需要布局,布局的只是后期会更新的. 提供一些图标的网站:font-awesome:     http://fontaw ...

  2. python开发_tkinter_获取文本框内容_给文本框添加键盘输入事件

    在之前的blog中有提到python的tkinter中的菜单操作 python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 python开发_tkinter_窗口控件_自 ...

  3. Python 45 长度及颜色单位 、字体样式 、文本样式 、背景样式 、css基础选择器

    一:长度及颜色单位   长度单位       px(像素)        in(英寸)       pt(点),一个标准的长度单位,1pt = 1/72in       mm(毫米)       cm ...

  4. Python开发【前端】:jQuery

    jQuery简介 jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨是&qu ...

  5. Python开发【前端】:DOM

    DOM(文档对象模型(Document Object Model)) 文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口.在网页 ...

  6. Python开发接水果小游戏

    我研发的Python游戏引擎Pylash已经更新到1.4了.如今我们就来使用它完毕一个极其简单的小游戏:接水果. 下面是游戏截图: 游戏操作说明:点击屏幕左右两边或者使用键盘方向键控制人物移动.使人物 ...

  7. python开发最受欢迎的十款工具

    python开发最受欢迎的十款工具 dreamyla3个月前 今天小编给正在学习python开发的朋友介绍十款最受欢迎的开发工具,因为在学习python开发过程中少不了IDE或者代码编辑器,想要提高开 ...

  8. python 开发工具简介

    一.python 开发工具简介 1.IDLE IDLE是开发python程序的基本IDE(集成开发环境),具备基本的IDE的功能,是非商业Python开发的不错的选择.当安装好python以后,IDL ...

  9. Python 开发工具推荐

    对于开发工具,仁者见仁智者见智,关键是自己喜欢,用着顺手就好,不用刻意去追求别人用的是什么工具. 这里给大家主要推荐三款工具,分别是PyCharm.Sublime Text 3.VS Code,因为这 ...

随机推荐

  1. Tornado/Python 学习笔记(一)

    1.源代码下载及安装:http://www.tornadoweb.org/en/stable/ 2.python中xmlrpc库官方文档:https://docs.python.org/3/libra ...

  2. koa通过get请求获取参数

    1.通过get方式请求获取参数的方式有两种 通过上下文获取 通过request获取 获得的格式有两种:query与querystring 注意:querystring为小写,驼峰格式会导致无法获取 2 ...

  3. appium===浮窗无法定位的解决办法

    这个问题比较常见,而且不被重视.大多数人的解决思路是根据坐标,把它点掉. 如下图,根本无法定位出“你好”这个元素: 最后的解决办法是: 在desired_caps={}中增加, desired_cap ...

  4. 微信access_token和refresh_token保存于redis

    简介 通常理解的access_token和refresh_token access_token是用来对客户端进行认证的,类似与密码,有一定的有效期.当过期后可使用refresh_token重新获取一个 ...

  5. 对 makefile 中 .DEFAULT 的理解

    上例子: all:gao @echo "final".DEFAULT: @echo "In default" 由于 gao 是一个前提条件,但是 makefil ...

  6. MySQL三种备份

    一)备份分类 1 2 3 4 5 6 7 8 9 10 11 12 冷备:cold backup数据必须下线后备份 温备:warm backup全局施加共享锁,只能读,不能写 热备:hot backu ...

  7. 深度学习方法(七):最新SqueezeNet 模型详解,CNN模型参数降低50倍,压缩461倍!

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入. 继续前面关于深度学习CNN经典模型的 ...

  8. 《HBase权威指南》学习笔记

    第一章  简介 背景: GFS:集群存储海量数据,数据在节点间冗余复制,即使一台存储服务器发生故障,也不会影响可用性. GFS的缺点:适合存储少许非常大的文件,而不适合存储大量小文件,因为文件的元数据 ...

  9. Java学习(final、static关键词)

    final关键词 概念:final的意思为最终,不可变.final是个修饰符,它可以用来修饰类,类的成员,以及局部变量.不能修饰构造方法. 特点: 1.final修饰的类不可以被继承,但可以继承别的类 ...

  10. Codeforces Round #292 (Div. 1) C - Drazil and Park

    C - Drazil and Park 每个点有两个值Li 和 Bi,求Li + Rj (i < j) 的最大值,这个可以用线段树巧妙的维护.. #include<bits/stdc++. ...