← Back to challenges

Truncate String at a Given Length

PythonHardstringsformatting

Instructions

Create a function that takes a string (the string to truncate) and a number (the maximum length of the truncated string) as arguments and returns the truncated string at the given length.

Examples

truncate("Lorem ipsum dolor sit amet.", 11) ➞ "Lorem ipsum"

truncate("Lorem ipsum dolor sit amet.", 16) ➞ "Lorem ipsum"

truncate("Lorem ipsum dolor sit amet.", 17) ➞ "Lorem ipsum dolor"

Notes

  • To "truncate" means "to shorten by cutting off the top or end".
  • If a word is truncated in the middle, discard it in the output (see 2nd example above).
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.