📜  python split 聪明的循环 - Python 代码示例

📅  最后修改于: 2022-03-11 14:47:19.558000             🧑  作者: Mango

代码示例1
text = "hello, this is some text to break up, with some reeeeeeeeeaaaaaaally long words."
n = 16

words = iter(text.split())
lines, current = [], next(words)
for word in words:
    if len(current) + 1 + len(word) > n:
        lines.append(current)
        current = word
    else:
        current += " " + word
lines.append(current)