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. 如何给cbv的程序添加装饰器

    引入method_decorator模块 1,直接在类上加装饰器 @method_decorator(test,name=‘dispatch’) class Loginview(view) 2,直接在 ...

  2. python flask 小项目

    0 开始之前 网上看了很多教程,都不是很满意,因此自己写一个大型教程,从入门到做出一个比较完整的博客.此次教程不是直接把整个博客直接代码整理出来然后运行一遍就完事,我会从flask的各个模块讲起.所以 ...

  3. springBoot AOP学习(一)

    AOP学习(一) 1.简介 AOp:面向切面编程,相对于OOP面向对象编程. Spring的AOP的存在目的是为了解耦.AOP可以让一切类共享相同的行为.在OOP中只能通过继承类或者实现接口,使代码的 ...

  4. SQL注入之Sqli-labs系列第五关和第六关(基于GET型的报错注入)

    废话不在多说  let's go! 开始挑战第五关(Double Query- Single Quotes- String) 和第六关(Double Query- Double Quotes- Str ...

  5. MySQL 显示表字段及注释等信息

    SHOW TABLES  from resource [FROM db_name] //列出数据库数据表 SHOW TABLE STATUS from resource [FROM db_name]  ...

  6. SpringBoot Maven项目 Helloworld 测试

    SpringBoot 化繁为简,简化配置 SpringBoot官方:http://projects.spring.io/spring-boot/SpringBoot使用介绍:http://blog.c ...

  7. 第七十四课 图的遍历(BFS)

    广度优先相当于对顶点进行分层,层次遍历. 在Graph.h中添加BFS函数: #ifndef GRAPH_H #define GRAPH_H #include "Object.h" ...

  8. Mac 终端添加代码到SVN

    从SVN拉取代码步骤: 1.cd  /Users/mark/zkh/Work/BC/(本地路径) 2.svn checkout  https://192.168.2.99/svn/bc_android ...

  9. Android SO UPX壳问题小记

    网上有篇 Android SO(动态链接库)UPX加固指南,详细介绍了如何使用UPX给Android SO加壳,尝试做了一下结果ok,这里只记录遇到的几个小问题. 1.40k以下so不能加壳 kiii ...

  10. 【leetcode】345. Reverse Vowels of a String

    problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...