import textwrap
s = """Look into my eyes, look into my eyes, the eyes, the eyes, the eyes,
not around the eyes, don't look around the eyes,
look into my eyes, you're under."""

使用textwrap.wrap()

# 使用textwrap.wrap(s[, width])进行段落划分为list
print(textwrap.wrap(s, width=15)) # 添加'width'属性,以单词为单位(包括字符)最大长度不超过15个字符
"""
D:\笔记\python电子书\Python3>python index.py
['Look into my', 'eyes, look into', 'my eyes, the', 'eyes, the eyes,', 'the eyes, not', 'around the', "eyes, don't", 'look around the', 'eyes, look into', "my eyes, you're", 'under.']
"""

使用textwrap.fill

# 使用textwrap.fill(s[, width])进行换行显示
print(textwrap.fill(s, width=55)) # 添加"width"属性,表示每一行以单词为单位(包括字符)最大长度不能超过width(55)
"""
D:\笔记\python电子书\Python3>python index.py
Look into my eyes, look into my eyes, the eyes, the
eyes, the eyes, not around the eyes, don't look around
the eyes, look into my eyes, you're under.
"""

使用textwrap.shorten()

# 使用textwrap.shorten(s, width[, placeholder])进行缩略显示
print(textwrap.shorten(s, width=20)) # 表示以单词为单位(包括字符)最长显示width,之后的内容以“[...]”方式缩略显示
print(textwrap.shorten(s, width=20, placeholder='...')) # 使用placeholder改变默认显示的方式" [...]"为"..."
"""
D:\笔记\python电子书\Python3>python index.py
Look into my [...]
"""

对文本内容进行缩进操作

import textwrap
s = """ Look into my eyes, look into my eyes, the eyes, the eyes, the eyes,
not around the eyes, don't look around the eyes,
look into my eyes, you're under."""

使用dedent()

# dedent()用来取消缩进,比较下面两个输出即可
print(s) # 原来定义的s每行前面都有空格
print(textwrap.dedent(s)) # 使用dedent()之后,有一行是顶格的,其它照着往前缩进
"""
D:\笔记\python电子书\Python3>python index.py
Look into my eyes, look into my eyes, the eyes, the eyes, the eyes,
not around the eyes, don't look around the eyes,
look into my eyes, you're under.
Look into my eyes, look into my eyes, the eyes, the eyes, the eyes,
not around the eyes, don't look around the eyes,
look into my eyes, you're under.
"""

使用indent()

# indent(s, prefix[, predicate=None])添加缩进
print(s) # 原来定义的s字符串
print(textwrap.indent(s, '--')) # 在每一行前面添加"--"
"""
D:\笔记\python电子书\Python3>python index.py
Look into my eyes, look into my eyes, the eyes, the eyes, the eyes,
not around the eyes, don't look around the eyes,
look into my eyes, you're under.
-- Look into my eyes, look into my eyes, the eyes, the eyes, the eyes,
-- not around the eyes, don't look around the eyes,
-- look into my eyes, you're under.
"""

textwrap 笔记的更多相关文章

  1. python核心编程第二版笔记

    python核心编程第二版笔记由网友提供:open168 python核心编程--笔记(很详细,建议收藏) 解释器options:1.1 –d   提供调试输出1.2 –O   生成优化的字节码(生成 ...

  2. WPF笔记(1.2 Navigation导航)——Hello,WPF!

    原文:WPF笔记(1.2 Navigation导航)--Hello,WPF! 这一节是讲导航的.看了一遍,发现多不能实现,因为版本更新了,所以很多旧的语法不支持了,比如说,不再有NavigationA ...

  3. python核心编程--笔记

    python核心编程--笔记 的解释器options: 1.1 –d   提供调试输出 1.2 –O   生成优化的字节码(生成.pyo文件) 1.3 –S   不导入site模块以在启动时查找pyt ...

  4. Python 工匠:使用数字与字符串的技巧学习笔记

    #Python 工匠:使用数字与字符串的技巧学习笔记#https://github.com/piglei/one-python-craftsman/blob/master/zh_CN/3-tips-o ...

  5. python3-cookbook笔记:第二章 字符串和文本

    python3-cookbook中每个小节以问题.解决方案和讨论三个部分探讨了Python3在某类问题中的最优解决方式,或者说是探讨Python3本身的数据结构.函数.类等特性在某类问题上如何更好地使 ...

  6. git-简单流程(学习笔记)

    这是阅读廖雪峰的官方网站的笔记,用于自己以后回看 1.进入项目文件夹 初始化一个Git仓库,使用git init命令. 添加文件到Git仓库,分两步: 第一步,使用命令git add <file ...

  7. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  8. SQL Server技术内幕笔记合集

    SQL Server技术内幕笔记合集 发这一篇文章主要是方便大家找到我的笔记入口,方便大家o(∩_∩)o Microsoft SQL Server 6.5 技术内幕 笔记http://www.cnbl ...

  9. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

随机推荐

  1. Node.js 回调函数 1) 阻塞 ,同步 2) 非阻塞 ,异步.

    1.阻塞. 同步. 1) 读取的文件: input.txt 菜鸟教程官网地址:www.runoob.com 2) main.js var fs = require("fs"); / ...

  2. ChinaCock界面控件介绍-CCNewsSilder

    上图是控件包里的Demo运行效果,轮播新闻图片. 这个控件用起来简单,拖放一个CCNewsSiler到Form上,设置Align为Top,再设置好高度,然后用代码加载图片: procedure TFo ...

  3. opendressinghash //use resize array

    public class opendressinghash<Key, Value> { private static final int INIT_CAPACITY = 4; privat ...

  4. express框架以及配置项

    以上命令会将 Express 框架安装在当前目录的 node_modules 目录中, node_modules 目录下会自动创建 express 目录.以下几个重要的模块是需要与 express 框 ...

  5. JAVA个人小程序GUI篇-收银(标签、按钮、复选框、下拉标、文本域、表格······)

    如果用eclipse需先装载windowsbuild //导入包 import java.awt.BorderLayout; import java.awt.EventQueue; import ja ...

  6. django,uwsgi, nginx部署项目

    在liunx中环境中 对于nginx来说: 1.先安装nginx sudo apt-get install nginx 2.启动nginx服务 sudo /etc/init.d/nginx resta ...

  7. HDU 1087:Super Jumping! Jumping! Jumping!(LIS)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  8. [zoj4045][思维+dfs]

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4045 题意:给一棵树.这棵树有n个节点,问你这个图能不能分成k个分块.这个 ...

  9. 【HDOJ1069】【动态规划】

    http://acm.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS (Java/Othe ...

  10. This Jenkins instance appears to be offline

    tomcat 方式启动jenkins时,报:This Jenkins instance appears to be offline and offers options to "Config ...