How do I write code in Python to do this? I want to read two strings that are multiple lines and much text, mainly to compare how similar they are (qualitatively.)
s1 = 'I want to read these texts side by side and see how similar they really are'
s2 = 'I really want to read these here texts side by side to see how similar they are (qualitatively)'
print_side_by_side(s1,s2)
Output:
I want to read these texts side by side and see ho I really want to read these here texts side by sid
w similar they really are e to see how similar they are (qualitatively)
Here's an approach using slicing:
def print_side_by_side(a, b, size=30, space=4):
while a or b:
print(a[:size].ljust(size) + " " * space + b[:size])
a = a[size:]
b = b[size:]
s1 = 'I want to read these texts side by side and see how similar they really are'
s2 = 'I really want to read these here texts side by side to see how similar they are (qualitatively)'
print_side_by_side(s1, s2)
Output:
I want to read these texts sid I really want to read these he
e by side and see how similar re texts side by side to see h
they really are ow similar they are (qualitati
vely)
This can be generalized to work on any number of strings:
def side_by_side(strings, size=30, space=4):
strings = list(strings)
result = []
while any(strings):
line = []
for i, s in enumerate(strings):
line.append(s[:size].ljust(size))
strings[i] = s[size:]
result.append((" " * space).join(line))
return "\n".join(result)
if __name__ == "__main__":
strings = "aaaaaaaa", "bbbbbbbbbbbbbb", "ccccccc"
print(side_by_side(strings, size=5, space=1))
Output:
aaaaa bbbbb ccccc
aaa bbbbb cc
bbbb
If you want to handle strings with newlines, either pre-split them in advance of calling this algorithm or try something like:
def side_by_side(strings, size=30, space=4):
strings = list(strings)
result = []
while any(strings):
line = []
for i, s in enumerate(strings):
buf = s[:size]
try:
n = buf.index("\n")
line.append(buf[:n].ljust(size))
strings[i] = s[n+1:]
except ValueError:
line.append(buf.ljust(size))
strings[i] = s[size:]
result.append((" " * space).join(line))
return "\n".join(result)
Note that if you're looking for more capability than this, Python has an industrial-strength solution for general diffing called difflib.