python 文本分析库_Python标准库——文本处理

悬挂缩进

不仅可以设置输出的宽度,还可以采用同样的方式单独控制首行的缩进,使首行的缩进不同于后续的各行。

新建textwrap_hanging_indent.py Python文件,在里面编写如下代码,查看输出结果。

import textwrap

from textwrap_example import sample_text

dedent_text = textwrap.dedent(sample_text).strip()

print(textwrap.fill(dedent_text,

initial_indent="",

subsequent_indent=" " * 4,

width=50,

))

以上代码输出结果为:

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.

以上代码允许生成悬挂缩进,即首行缩进小于其他行的缩进。

缩进也可以包含非空白字符。例如,悬挂缩进可以加前缀*来生成圆点项目符号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值