align strings over a common character

Based on the information you provided, this may work:

lst = [
'1 + 2 = 3',
'2*6 + 13 = 25',
'2*6 = 12',
'2 = 12 - 10'
]

mxleft = max([e.index('=') for e in lst])

l2 = [e.split('=')[0].rjust(mxleft) + '=' + e.split('=')[1] for e in lst]

print('\n'.join(l2))

Output

   1 + 2 = 3
2*6 + 13 = 25
     2*6 = 12
       2 = 12 - 10

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top