Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python truncate a long string

Tags:

python

People also ask

How do I limit the length of a character in Python?

use this: while True: answer = input("What's up, doc? ") if len(answer) >= 10: print("Too much info - keep it shorter!

How do you cut a string after a specific character in Python?

To remove everything after the first occurrence of the character '-' in a string, pass the character '-' as separator and 1 as the max split value. The split('-', 1) function will split the string into 2 parts, Part 1 should contain all characters before the first occurrence of character '-'.


info = (data[:75] + '..') if len(data) > 75 else data

Even shorter :

info = data[:75] + (data[75:] and '..')

Even more concise:

data = data[:75]

If it is less than 75 characters there will be no change.


If you are using Python 3.4+, you can use textwrap.shorten from the standard library:

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:

>>> textwrap.shorten("Hello  world!", width=12)
'Hello world!'
>>> textwrap.shorten("Hello  world!", width=11)
'Hello [...]'
>>> textwrap.shorten("Hello world", width=10, placeholder="...")
'Hello...'

For a Django solution (which has not been mentioned in the question):

from django.utils.text import Truncator
value = Truncator(value).chars(75)

Have a look at Truncator's source code to appreciate the problem: https://github.com/django/django/blob/master/django/utils/text.py#L66

Concerning truncation with Django: Django HTML truncation


With regex:

re.sub(r'^(.{75}).*$', '\g<1>...', data)

Long strings are truncated:

>>> data="11111111112222222222333333333344444444445555555555666666666677777777778888888888"
>>> re.sub(r'^(.{75}).*$', '\g<1>...', data)
'111111111122222222223333333333444444444455555555556666666666777777777788888...'

Shorter strings never get truncated:

>>> data="11111111112222222222333333"
>>> re.sub(r'^(.{75}).*$', '\g<1>...', data)
'11111111112222222222333333'

This way, you can also "cut" the middle part of the string, which is nicer in some cases:

re.sub(r'^(.{5}).*(.{5})$', '\g<1>...\g<2>', data)

>>> data="11111111112222222222333333333344444444445555555555666666666677777777778888888888"
>>> re.sub(r'^(.{5}).*(.{5})$', '\g<1>...\g<2>', data)
'11111...88888'

You could use this one-liner:

data = (data[:75] + '..') if len(data) > 75 else data