Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Cut Sentence by vtflnk
from textwrap import shorten
def cut_sentence(line: str, length: int) -> str:
"""
Cut a given sentence, so it becomes shorter than or equal to a given length.
"""
if len(line) > length:
return shorten(line, width=length, placeholder="", break_long_words=False) + "..."
return line
print("Example:")
print(cut_sentence("Hi my name is Alex", 4))
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!")
Jan. 2, 2023