我有一个字符串,我想分成 N 个相等的部分。
例如,假设我有一个长度为 128 的字符串,我想将它分成 4 个长度为 32 的 block ;即,前 32 个字符,然后是第二个 32,依此类推。
我该怎么做?
最佳答案
import textwrap
print(textwrap.wrap("123456789", 2))
#prints ['12', '34', '56', '78', '9']
注意:注意空格等 - 这可能是也可能不是你想要的。
"""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.
"""
关于python - 将一个字符串分成N个相等的部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22571259/