Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
one string solution solution in Clear category for Cut Sentence by kdim
def cut_sentence(s,l):
return s if len(s)<=l else " ".join((s[:l+1]+".").split()[:-1])+"..."
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"
assert cut_sentence("Hi my name is Alex", 10) == "Hi my name..."
assert cut_sentence("Hi my name is Alex", 11) == "Hi my name..."
print('Done! Do you like it? Go Check it!')
Jan. 9, 2021
Comments: