Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Cut Sentence by TheRing
def cut_sentence(line: str, length: int) -> str:
if len(line) <= length:
return line
else:
try:
return line[0:line.rindex(' ', 0, length+1)] + '...'
except ValueError:
return '...'
print("Example:")
print(cut_sentence("Hi my name is Alex", 4))
print(cut_sentence("Hi my name is Alex", 10))
# These "asserts" are used for self-checking
assert cut_sentence("Hi my name is Alex", 8) == "Hi my..."
assert cut_sentence("Hi my name is Alex", 4) == "Hi..."
assert cut_sentence("Hi my name is Alex", 20) == "Hi my name is Alex"
assert cut_sentence("Hi my name is Alex", 18) == "Hi my name is Alex"
print("The mission is done! Click 'Check Solution' to earn rewards!")
March 11, 2023
Comments: