Python Forum

Full Version: truncating a string and adding ellipses
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
in a set of functions i am writing there is one that outputs a name and data value given to it. the name usually a string and the data is often a string. or it might be some huge object. it tries to print them on one line. if they don't fit, it truncates the output line, which means it is no longer viable as python syntax. if truncated i append "..." with that truncation being 3 more characters so it fits. it also means no quote is printed since the truncation applies the line after repr() or !r in f-stribgs has inclode quotes.

what i wonder is if i should insert a closing quote when truncated or just not worry about it. a like might look like:

this example truncates at column 24:
Output:
'alpha':'abcdefghijkl...
code to make the final form might look lik:
...
out=f'{name!r}:{value!r}'
if len(out)>width:
    out=out[:width-3]+'...'
print(out)
...
if the above output is not good enough, how should it be?