45

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 really awesome."
"Dumb" truncate:   "This is real..."
"Smart" truncate:  "This is really..."

I'm looking for a way to accomplish the "smart" truncate from above.

Jack
  • 20,735
  • 11
  • 48
  • 48

10 Answers10

69

I actually wrote a solution for this on a recent project of mine. I've compressed the majority of it down to be a little smaller.

def smart_truncate(content, length=100, suffix='...'):
    if len(content) <= length:
        return content
    else:
        return ' '.join(content[:length+1].split(' ')[0:-1]) + suffix

What happens is the if-statement checks if your content is already less than the cutoff point. If it's not, it truncates to the desired length, splits on the space, removes the last element (so that you don't cut off a word), and then joins it back together (while tacking on the '...').

Adam
  • 7,067
  • 2
  • 27
  • 24
  • This is really concise... I'd add one more test to avoid empty strings in case there are no spaces at all in the first "length" characters. – Jonas Aug 22 '11 at 12:36
  • 7
    The truncation had to consider the suffix length : `return ' '.join(content[:length+1-len(suffix)].split(' ')[0:-1]) + suffix` – Stan Feb 20 '12 at 09:36
  • There's a corner case here that could bite someone: if `content[:length+1]` happens to end in a space, the returned string will be longer than `length`. The same goes for `content[:length+1-len(suffix)` from @Stan's comment. – coredumperror Apr 24 '18 at 20:16
  • 1
    @Adam Nicely answered over 11 years ago and yet so enduring. Thanks for saving us a lot of searching and code mistakes :-) – blueDroid Nov 25 '19 at 20:41
  • This is old, but useful. Would suggest maybe adding a rstrip after the join? `' '.join(content[:length+1].split(' ')[0:-1]).rstrip() + suffix` otherwise you can end up with something like `'hello how are you todiajhsdfaja ...'` – Curt Dec 08 '20 at 18:12
48

Here's a slightly better version of the last line in Adam's solution:

return content[:length].rsplit(' ', 1)[0]+suffix

(This is slightly more efficient, and returns a more sensible result in the case there are no spaces in the front of the string.)

bobince
  • 528,062
  • 107
  • 651
  • 834
  • That's interesting about the rsplit. I guess I never ran across it. – Adam Oct 30 '08 at 14:53
  • A quick test of the two approaches (Python 2.4.3): Adam's code: >>> smart_truncate('The quick brown fox jumped over the lazy dog.', 26) "The quick brown fox jumped..." With bobince's code: >>> smart_truncate('The quick brown fox jumped over the lazy dog.', 26) The quick brown fox... – Patrick Cuff Oct 30 '08 at 15:06
  • Yeah, I added in length+1 on the truncation to handle if the truncation splits exactly at the end of a word naturally. – Adam Oct 30 '08 at 15:10
  • 1
    This one is better. But I'd put it under the if and skip the else, it's more pythonix. – Bite code Nov 01 '08 at 17:31
  • 1
    Well, then, let's use the conditional expression: def smart_truncate(content, length=100, suffix='...'): return (content if len(content) <= length else content[:length].rsplit(' ', 1)[0]+suffix) – hughdbrown Mar 05 '09 at 00:54
  • 2
    So let's be sure, the resulting string is not longer then length: `return content if len(content) <= length else content[:length-len(suffix)].rsplit(' ', 1)[0] + suffix` – kraiz Dec 12 '11 at 07:12
11

There are a few subtleties that may or may not be issues for you, such as handling of tabs (Eg. if you're displaying them as 8 spaces, but treating them as 1 character internally), handling various flavours of breaking and non-breaking whitespace, or allowing breaking on hyphenation etc. If any of this is desirable, you may want to take a look at the textwrap module. eg:

def truncate(text, max_size):
    if len(text) <= max_size:
        return text
    return textwrap.wrap(text, max_size-3)[0] + "..."

The default behaviour for words greater than max_size is to break them (making max_size a hard limit). You can change to the soft limit used by some of the other solutions here by passing break_long_words=False to wrap(), in which case it will return the whole word. If you want this behaviour change the last line to:

    lines = textwrap.wrap(text, max_size-3, break_long_words=False)
    return lines[0] + ("..." if len(lines)>1 else "")

There are a few other options like expand_tabs that may be of interest depending on the exact behaviour you want.

Brian
  • 116,865
  • 28
  • 107
  • 112
9
>>> import textwrap
>>> textwrap.wrap('The quick brown fox jumps over the lazy dog', 12)
['The quick', 'brown fox', 'jumps over', 'the lazy dog']

You just take the first element of that and you're done...

Anthony
  • 12,407
  • 12
  • 64
  • 88
  • 3
    `textwrap.shorten("Hello world", width=10, placeholder="...")` would produce `"Hello..."` https://docs.python.org/3.5/library/textwrap.html – Salami Dec 10 '15 at 16:43
  • I just tried this one and it broke in the middle of a grapheme cluster, so it isn't even doing proper character breaking, let alone word breaking. – Hakanai May 02 '17 at 23:21
9
def smart_truncate1(text, max_length=100, suffix='...'):
    """Returns a string of at most `max_length` characters, cutting
    only at word-boundaries. If the string was truncated, `suffix`
    will be appended.
    """

    if len(text) > max_length:
        pattern = r'^(.{0,%d}\S)\s.*' % (max_length-len(suffix)-1)
        return re.sub(pattern, r'\1' + suffix, text)
    else:
        return text

OR

def smart_truncate2(text, min_length=100, suffix='...'):
    """If the `text` is more than `min_length` characters long,
    it will be cut at the next word-boundary and `suffix`will
    be appended.
    """

    pattern = r'^(.{%d,}?\S)\s.*' % (min_length-1)
    return re.sub(pattern, r'\1' + suffix, text)

OR

def smart_truncate3(text, length=100, suffix='...'):
    """Truncates `text`, on a word boundary, as close to
    the target length it can come.
    """

    slen = len(suffix)
    pattern = r'^(.{0,%d}\S)\s+\S+' % (length-slen-1)
    if len(text) > length:
        match = re.match(pattern, text)
        if match:
            length0 = match.end(0)
            length1 = match.end(1)
            if abs(length0+slen-length) < abs(length1+slen-length):
                return match.group(0) + suffix
            else:
                return match.group(1) + suffix
    return text
Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
4

From Python 3.4+ you can use textwrap.shorten. With the OP example:

>>> import textwrap
>>> original = "This is really awesome."
>>> textwrap.shorten(original, width=20, placeholder="...")
'This is really...'

textwrap.shorten(text, width, **kwargs)

Collapse and truncate the given text to fit in the given width.

First the whitespace in text is collapsed (all whitespace is replaced by single spaces). If the result fits in the width, it is returned. Otherwise, enough words are dropped from the end so that the remaining words plus the placeholder fit within width:

marcanuy
  • 23,118
  • 9
  • 64
  • 113
3
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...'
Vebjorn Ljosa
  • 17,438
  • 13
  • 70
  • 88
  • Note: If width > len(s), you get an out of bounds on s[width]. You probably want an extra check for the case where truncation isn't needed. – Brian Oct 30 '08 at 15:36
0

For Python 3.4+, I'd use textwrap.shorten.

For older versions:

def truncate(description, max_len=140, suffix='…'):    
    description = description.strip()
    if len(description) <= max_len:
        return description
    new_description = ''
    for word in description.split(' '):
      tmp_description = new_description + word
      if len(tmp_description) <= max_len-len(suffix):
          new_description = tmp_description + ' '
      else:
          new_description = new_description.strip() + suffix
          break
    return new_description
Jorge Barata
  • 2,227
  • 1
  • 20
  • 26
0

In case you might actually prefer to truncate by full sentence rather than by word, here's something to start with:

def smart_truncate_by_sentence(content, length=100, suffix='...',):
    if not isinstance(content,str): return content
    if len(content) <= length:
        return content
    else:
        sentences=content.split('.')
        cs=np.cumsum([len(s) for s in sentences])
        n = max(1,  len(cs[cs<length]) )
        return '.'.join(sentences[:n])+ '. ...'*(n<len(sentences))
CPBL
  • 3,783
  • 4
  • 34
  • 44
0

C++ version:

string trim(string s, int k) {
    if (s.size()<=k) return s;
    while(k>=0 && s[k]!=' ')
        k--;
    if (k<0) return "";
    string res=s.substr(0, k+1);
    while(res.size() && (res.back()==' '))
        res.pop_back();
    return res;    
}
Claud
  • 1