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. JavaWeb:c3p0配置问题java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector

    错误显示 java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector at dbdemo.JdbcUtils.<clinit> ...

  2. [转]JavaScript与元素间的抛物线轨迹运动

    在张鑫旭的博客看到这个抛物线的小动画,觉得很感兴趣,转载一下方便研究~ 原文地址:http://www.zhangxinxu.com/wordpress/?p=3855 在页面上添加元素的位移动画,除 ...

  3. Python学习笔记第十二周

    目录: 数据库介绍 mysql 数据库安装使用 mysql管理 mysql 数据类型 常用mysql命令事务 索引 创建数据库 外键 增删改查表 权限 python 操作mysql  ORM sqla ...

  4. vue查缺补漏题

    一.对于MVVM的理解? MVVM 是 Model-View-ViewModel 的缩写.Model代表数据模型,也可以在Model中定义数据修改和操作的业务逻辑.View 代表UI 组件,它负责将数 ...

  5. python开发day03

    一.常见的数据类型 1. int ==> 整数. 主要⽤用来进⾏行行数学运算  \ (常见的操作有+-*%) a.bit_length() a= # 10进制 二进制 100 print(a.b ...

  6. 51Nod 1240:莫比乌斯函数

    1240 莫比乌斯函数  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使 ...

  7. ZOJ 1007:Numerical Summation of a Series(数学)

    Numerical Summation of a Series Time Limit: 10 Seconds      Memory Limit: 32768 KB      Special Judg ...

  8. 网络IP和网络掩码以及网关的学习

    192.168.10.1/24,192.168.1.1/30是什么意思啊? 表示地址范围你把这些前面的值转换成2进制 就是变成一个32位的地址比如192.168.10.1就变成11000000.101 ...

  9. Java打印九九乘法表及倒打九九乘法表

    //正打 public class Test3 { public static void main(String[] args) { for(int j=1;j<10;j++){ for(int ...

  10. python 基础的用法新发现

    引用的发现: # 引用的处理 因为变量指的是某一内存地址 引用变量实际是引用一个固定的内存地址,# 当这个变量中的内容变了之后,他的内存地址不变, 所以引用者也动态的得到了变化的变量l1=[1,2,3 ...