Created
June 28, 2011 03:48
-
-
Save dqminh/1050448 to your computer and use it in GitHub Desktop.
basic word wrap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def word_wrap(input, width): | |
input = input.strip() | |
if not input: | |
return "" | |
if len(input) < width: | |
return input | |
i = input.find(" ", width) | |
if i < 0: | |
return input[:width] + "\n" + word_wrap(input[width:], width) | |
else: | |
return input[:i] + "\n" + word_wrap(input[i:], width) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment