12345678910111213141516171819202122232425262728293031 |
- """
- Write a function called truncate that will shorten a string
- to a specified length, and add "..." to the end. Given a
- string and a number, truncate the string to a shorter string
- containing at most n characters. For example,
- truncate("long string", 5) should return a 5 character truncated
- version of "long string". If the string gets truncated, the
- truncated return string should have a "..." at the end. Because
- of this, the smallest number passed in as a second argument should
- be 3.
- """
- def truncate(text: str, trunc: int):
- if trunc < 3:
- return "Truncation must be at least 3 characters."
- return text[0:trunc-3] + "..." if trunc <= len(text) else text
- print(truncate("Hello World", 6))
- '''
- truncate("Super cool", 2) # "Truncation must be at least 3 characters."
- truncate("Super cool", 1) # "Truncation must be at least 3 characters."
- truncate("Super cool", 0) # "Truncation must be at least 3 characters."
- truncate("Hello World", 6) # "Hel..."
- truncate("Problem solving is the best!", 10) # "Problem..."
- truncate("Another test", 12) # "Another t..."
- truncate("Woah", 4) # "W..."
- truncate("Woah", 4) # "..."
- truncate("Yo",100) # "Yo"
- truncate("Holy guacamole!", 152) # "Holy guacamole!"
- '''
|