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..."
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules