Last active
September 17, 2021 14:39
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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