AdvancedChallenge 30 of 34

Word wrap

Write wrap(text, width) that text into lines no longer than width characters, breaking only at spaces, greedily fitting as many words per line as possible. Words longer than width go on their own line unbroken. Return the of lines.

Examples
wrap("keep calm and write more code", 10) → ["keep calm", "and write", "more code"]
Exercise

Word wrap

4 checks
def wrap(text, width):
    ...


for line in wrap("keep calm and write more code", 10):
    print(f"|{line:<10}|")
 
Python (loads on first run)Ctrl+Enter runs
Output
Press Run to try your code, then Check answer to grade it.