Editor settings

General settings

Save settings

Learn to program with Python

String Truncation #

Write a function that truncates a given string to a specified length. Truncation means reducing the length of the string by removing characters from the end and adding an ellipsis "..." to indicate that the string has been shortened.

The function takes 2 arguments: the string to be truncated, and the maximum length allowed. If the string is shorter than the allowed length, no truncation is required.


Tests #

  • truncate("Hello, World!", 8) should return "Hello, W..."
  • truncate("The quick brown fox jumps over the lazy dog", 20) should return "The quick brown fox ..."
  • truncate("Lorem ipsum dolor sit amet", 25) should return "Lorem ipsum dolor sit ame..."
  • truncate("Python programming is fun", 30) should return "Python programming is fun"
  • truncate("abcdefghijklmnopqrstuvwxyz", 5) should return "abcde..."
Output will be displayed here