I am looking for a way to truncate a string in Python that will not cut off the string in the middle of a word.
For example:
Original: \"This is
def smart_truncate(s, width): if s[width].isspace(): return s[0:width]; else: return s[0:width].rsplit(None, 1)[0]
Testing it:
>>> smart_truncate('The quick brown fox jumped over the lazy dog.', 23) + "..." 'The quick brown fox...'