Python Forum
truncating a string and adding ellipses
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
truncating a string and adding ellipses
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020