Python Forum
truncating a string and adding ellipses - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: truncating a string and adding ellipses (/thread-28569.html)



truncating a string and adding ellipses - Skaperen - Jul-24-2020

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?