Skip to content

Instantly share code, notes, and snippets.

@mitchellrj
Created September 26, 2011 14:15
Show Gist options
  • Save mitchellrj/1242331 to your computer and use it in GitHub Desktop.
Save mitchellrj/1242331 to your computer and use it in GitHub Desktop.
Wrap text in Python
def wrap_text(text, limit):
#s = map(str.split, text.split('\n'))
#i = 0
#for n in range(len(s)):
# while i < len(s[n]) - 1:
# if (len(s[n][i]) + len(s[n][i+1])) <= limit:
# s[n][i] += ' ' + s[n][i+1]
# s[n] = s[n][:i+1] + s[n][i+2:]
# elif len(s[n][i+1]) > limit:
# i += 1
# i+=1
# s[n] = '\n'.join(s[n])
#return '\n'.join(s)
import textwrap
return textwrap.TextWrapper(width=limit, break_long_words=False).fill(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment