>>> from textwrap import wrap
>>> s = '1234567890'
>>> wrap(s, 2)
['12', '34', '56', '78', '90']
这就是wrap的docstring所说的:
>>> help(wrap)
'''
Help on function wrap in module textwrap:
wrap(text, width=70, **kwargs)
Wrap a single paragraph of text, returning a list of wrapped lines.
Reformat the single paragraph in 'text' so it fits in lines of no
more than 'width' columns, and return a list of wrapped lines. By
default, tabs in 'text' are expanded with string.expandtabs(), and
all other whitespace characters (including newline) are converted to
space. See TextWrapper class for available keyword args to customize
wrapping behaviour.
'''
为了完整起见,您可以使用regex执行此操作:
对于奇数个字符,可以执行以下操作:
您还可以执行以下操作,以将regex简化为更长的块:
如果字符串很长,可以使用
re.finditer
逐块生成。在python中已经有一个内置函数用于此目的。
这就是wrap的docstring所说的:
相关问题 更多 >
编程相关推荐