python - 将一个字符串分成N个相等的部分?

标签 python

我有一个字符串,我想分成 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/

相关文章:

Python MySQL 不更新字段或返回错误

python - imshow 更改图像大小后绘制点

python - Google App Engine self.redirect 帖子

python - 如何重新抽样 6 个月

python - 重新打开文件以进行追加

python - 使用 pandas GroupBy 检查组中的所有元素是否相等

python - 传递给其他命令时编码发生变化?

python - 递归中的返回语句

python - 重命名外部模块类方法

python - PyQt 从非 QThread 中唤醒主线程