Skip to content

Instantly share code, notes, and snippets.

@timonweb
Created August 1, 2013 17:00
Show Gist options
  • Select an option

  • Save timonweb/6133239 to your computer and use it in GitHub Desktop.

Select an option

Save timonweb/6133239 to your computer and use it in GitHub Desktop.
Truncate string on a word boundary
def smart_truncate(content, length=100, suffix='...'):
if len(content) <= length:
return content
else:
return ' '.join(content[:length+1].split(' ')[0:-1]) + suffix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment