Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
textwrap from standard lib - after veky's wonderful solution! solution in Clear category for Cut Sentence by piter239
# after seein https://py.checkio.org/mission/cut-sentence/publications/veky/python-3/tweak/?ordering=most_voted&filtering=all
# veky's wonderful solution
def cut_sentence(s, length):
if len(s) <= length:
return s
from textwrap import shorten
return shorten(s, width=length, placeholder='', break_long_words=False) + '...'
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert cut_sentence("Hi my name is Alex", 4) == "Hi...", "First"
assert cut_sentence("Hi my name is Alex", 8) == "Hi my...", "Second"
assert cut_sentence("Hi my name is Alex", 18) == "Hi my name is Alex", "Third"
assert cut_sentence("Hi my name is Alex", 20) == "Hi my name is Alex", "Fourth"
print('Done! Do you like it? Go Check it!')
April 25, 2020
Comments: