steve+comp.lang.python at pearwood.info
Wed Aug 24 03:24:14 CEST 2011
> Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
> [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> print "%.5s" % ("hello there, truncate me!")
Well, whadda you know, I learned something new :)
In any case, this doesn't solve the OP's problem, as he wants to truncate
the input string, and append '...' if and only if it were truncated.
The right solution is to wrap the functionality in a function. It's not
hard, and is elegant. Not everything needs to be a built-in.
def truncate(s, maxwidth=50):
if len(s) <= maxwidth:
s = s[:maxwidth - 3]
return s + '...'
More information about the Python-list