Python function for capping a string to a maximum length

Viewed 10255

Is there a function in Python, built-in or in the standard library, for capping a string to a certain length, and if the length was exceeded, append three dots (...) after it?

For example:

>>> hypothetical_cap_function("Hello, world! I'm a string", 10)
"Hello, ..."
>>> hypothetical_cap_function("Hello, world! I'm a string", 20)
"Hello, world! I'm..."
>>> hypothetical_cap_function("Hello, world! I'm a string", 50)
"Hello, world! I'm a string"
2 Answers
Related