Skip to content

Instantly share code, notes, and snippets.

@dqminh
Created June 28, 2011 03:48
Show Gist options
  • Save dqminh/1050448 to your computer and use it in GitHub Desktop.
Save dqminh/1050448 to your computer and use it in GitHub Desktop.
basic word wrap
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