Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Cut Sentence by MBM_1607
def cut_sentence(line, length):
'''
Cut a given sentence, so it becomes shorter than or equal to a given length.
'''
if len(line) <= length:
return line
else:
answer = ''
words = line.split(" ")
for word in words:
length -= len(word)
if length >= 0:
length -= 1
answer += word
answer += ' '
return answer.strip() + '...'
April 9, 2018
Comments: