Skip to content

Instantly share code, notes, and snippets.

@enricodvn
Last active September 17, 2021 14:39
Show Gist options
  • Save enricodvn/600a98d776ffdd6b12559c4f88289301 to your computer and use it in GitHub Desktop.
Save enricodvn/600a98d776ffdd6b12559c4f88289301 to your computer and use it in GitHub Desktop.
Text side by side in form of columns. Column width is given as the widest one.
from itertools import zip_longest
def text_side_by_side(*texts, separator="\t"):
max_length = 0
texts_lines = []
for text in texts:
# finds the widest one
text_lines = text.splitlines()
texts_lines.append(text_lines)
for line in text_lines:
max_length = max(len(line), max_length)
result_text = ""
for lines in zip_longest(*texts_lines, fillvalue=''):
for line in lines:
result_text += f"{line.strip().ljust(max_length)}{separator}"
result_text = f"{result_text[:-len(separator)]}\n"
return result_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment