Report abuse

1
2
3
4
5
6
7
8
9
10
11
def format(text, indent='', width=70):
    out = []
    stack = [word for word in text.replace("\n", " ").split(" ") if word]
    while stack:
        line = []
        while stack:
            if len(line) + len(" " + stack[0]) > width:
                break
            line.append(stack.pop(0))
        out.append(indent + " ".join(line))
    return "\n".join(out)