Source:
def truncate_middle(s, limit=20, replace_with='...'):
    s_len = len(s)
    if s_len <= limit:
        return s
    else:
        diff = (s_len - limit) / 2
        s_mid = s_len / 2
        return '%s%s%s' % (s[:s_mid - diff], replace_with, s[s_mid + diff:])
Sample Usage:
print truncate_middle('Hello World Im a long sentence')
# Hello Worl...g sentence
print truncate_middle('Hello World Im a long sentence', 10)
# Hello...tence

Truncates long text from the center/middle.

Pythoneer 6 years ago