1

Assuming I have a number of strings, each requiring a line break when printed, how can I vertically align them without giving a fixed number of characters per line? Example: String:

aaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbb

they are too long to be printed on one line each, so in the output a line break is introduced like this:

aaaaaaaaaaaaaaa
aaa
bbbbbbbbbbbbbbb
bbb

but I would like to have:

aaaaaaaaaaaaaaa
bbbbbbbbbbbbbbb
aaa
bbb

I store chars as a list.

lhcgeneva
  • 1,981
  • 2
  • 21
  • 29
  • 8
    Sort by length? – Simeon Visser Nov 25 '13 at 17:11
  • What data structure are you using to store these strings? – its me Nov 25 '13 at 17:15
  • How would sorting by length help? Both strings are of similar length, it's just about how they are displayed... – lhcgeneva Nov 25 '13 at 17:28
  • 1
    You really want to break up the strings after the line break, continuing with partial strings??? In your example aaaaaaaa is half-way printed, then bbbbbbb, then the rest of aa and bb. Sorting by length would achieve that. – dornhege Nov 25 '13 at 17:30
  • this is precisely what I would like to do! – lhcgeneva Nov 25 '13 at 17:30
  • Then split the strings to the display length and sort. Python's sort should be stable, so the result will be what you want. – dornhege Nov 25 '13 at 17:32
  • when you say you store chars as a list.. do you mean you do this just prior to output? And is this a must? – Totem Nov 25 '13 at 17:33
  • @dornhege I was looking for a command like find_display_length, but couldn't find any, what would you suggest? – lhcgeneva Nov 25 '13 at 17:35
  • Have your font a fixed width ? – Hacketo Nov 25 '13 at 17:40
  • Yep, you mean using sth like console.getTerminalSize() and then divide by the width? – lhcgeneva Nov 25 '13 at 17:42
  • OK, so your actual question is how to find the display width if you print to a terminal? – dornhege Nov 25 '13 at 17:54
  • What will determine the length of these strings before a line break is introduced? – Totem Nov 25 '13 at 17:54
  • @lhcgeneva the only way to accomplish that is via terminal commands. If you want fixed length strings sorted the way you specified, you need to have a fixed length in the code otherwise you have to use terminal commands to set the output – smac89 Nov 25 '13 at 17:54
  • http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python – dornhege Nov 25 '13 at 17:55
  • If there is no cooler automated way (putting things in a table, letting the table do the line break or similar) then yes, this is my question – lhcgeneva Nov 25 '13 at 17:55

0 Answers0